I have a requirement to match the 1s OR (AND) 0s using a regular expression.
For example
^((1+|0+)|(1*,0*))1((1+|0+)|(1*,0*))1+((1+|0+)|(1*,0*))$
matches 01001110.
I would like to know how to match "either series of 1 or series of 0 or mixed series of 1 and 0"
I have tried the following and doesn't work for string 1010.
^((1+|0+)|(1*,0*))1+((1+|0+)|(1*,0*))$
The idea is that in this problem, "A"s can be represented as 1+ OR 0+ or mix of both.
So I'm trying to derive the regular expression as shown above.
I am not sure if that is what you want but if you want to find series which contains only 1 and/or 0 then your regex can look like (1|0)+ which can be also written using character class as [01]+
If you're just matching binary digits, you can use:
Pattern any = Pattern.compile("[01]+");
If you want alternate binary digits, you can use:
Pattern alt = Pattern.compile("((?<=0|^)1|1(?=0|$))+");
Is this what you need?
^([0,1]+)$
Try this site for further tests:
http://www.regexplanet.com/advanced/java/index.html
Related
I have 2 strings "0000000000ABCDEF" and "1234567890ABCDEF" and I'm trying to find out how to capture "1234567890ABCDEF" using regular expression which has a rule that the first 10 characters must not be all zeroes "0".
Edit:
Thanks for all the useful comments so far.
My apologies if there is any confusion, by capture I mean to match a regular expression with "1234567890ABCDEF". And the same regular expression should not match "0000000000ABCDEF", therefore I felt that the design I'm trying to come up with should contain a rule that checks:
1) the first 10 characters cannot be all zeroes
I tried something like this (?!0{10}).* but it atill matches "0000000000ABCDEF".
I guess I'll read up more on regular expressions.
You should just be able to use a negative look behind like this:
(?<!0{10})ABCDEF
Here is a regex101 for you to see it working: https://regex101.com/r/l7pX8c/1
Can anyone tell how I can write regex for a string that take one or more alphanumeric character followed by an even number of digits?
Valid:
a11a1121
bbbb11a1121
Invalid:
a11a1
I have tried ^[a-zA-Z*20-9]*$ but it is always giving true.
Can you please help in this regard?
The regex that you have mentioned will search for any number of [either a-z, or A-Z or 2 or 0-9]
You can break down your requirement to groups and then handle it accordingly.
Like you require at least one character. so you start with ^([a-zA-Z]+)$
Then you need numbers in the multiple of 2. so you add ^([a-zA-Z]+(\d\d)+)$
Now you need any number of combination of these. So the exp becomes: ^([a-zA-Z]+(\d\d)+)*$
You can use online tools like regex101 for these purpose. The provided regex in action here
You can achieve it with this regexp: ^[a-z0-9]*[a-z]+([0-9]{2})*$
Explanation :
[a-z0-9]*[a-z]+: a string of at least one character terminated by a non digit one
([0-9]{2})*: an odd sequence of digits (0 or 2*n digits). If the even sequence cannot be null, use ([0-9]{2})+ instead.
Example
The no.s 1234 65
Input: n
For n=4, the output should be 1234
For n=2, the output should be : 65 (not 12)
Tried \d{n} which gives 12 and \d{n,} gives 1234 but i want the exact matching one.
Pattern p = Pattern.compile("//\d{n,}");
you need negative lookaround assertion: (?<!..): negative look behind, and (?!..): negative look ahead : regex101
(?<!\d)\d{4}(?!\d)
however not all regex engine supports them, maybe a work around may match also the preceeding character and following character (contrary to look-around which are 0 width matches), (\D matches all excpet a digit)
(?:^|\D)(\d{4})(?:\D|$)
I think what you meant is the \b character.
Hence, the regex you're looking for would be (for n=2):
\b\d{2}\b
From what I understand, you're looking for a regex that will match a number in a string which has n digits, taking into into account the spacing between the numbers. If that's the case, you're looking for something like this:
\b\d{4}\b
The \b will ensure the match is constrained to the start/end of a 'word' where a word is the boundary between anything matched by \w (which includes digits) and anything matched by the opposite, \W (which includes spaces).
I don't code in java but I can try to answer this using regex in general.
If your number is in the format d1d2d3d4 d5d6 and you want to extract digits d5d6, create 3 groups as r'([0-9]+)("/s")([0-9]+)' – each set of parenthesis () represent one group. Now, extract the third group only in another object which is your required output.
I have this regex expression written that should extract toll-free numbers but when there is a number like 1-800-343-2432 (when there is a 1 before the 800 stuff) it doesn't work
(?!(\$|#|800|855|866|877|888))\(?[\\s.-]*([0-9]{3})?[\\s.-]*\)?[\\s.-]*[0-9]{3}[\\s.-]*[0-9]{4}
how can i modify this expression to not take numbers like 1-866-343-1232 too ?!
Without checking your full regex you can use this regex to block 1-888:
(?!(?:1-)?(\\$|#|800|855|866|877|888))\(?[\\s.-]*([0-9]{3})?[\\s.-]*\)?[\\s.-]*[0-9]{3}[\\s.-]*[0-9]{4}
Prepend (1-)? to your regex. This will work for optional 1-.
Modifying your regular expression:
(\+)?(1-)?\(?(\\$|#|800|855|866|877|888)\)?[\\s.-]*([0-9]{3})?[\\s.-]*\)?[\\s.-]*[0-9]{3}[\\s.-]*[0-9]{4}
The key differences here are the following:
(\+)? :: A lazy quantifier `?' matches a + character if it takes place prior to the 1. Many numbers display like +1-800-343-2432
(1-)? :: Matches a 1 followed by a - character. The ? is a lazy quantifier that matches the 1- if it exists.
And I also added \(? and \)? which allow you to match on numbers that present in the format +1-(800)-343-2432
I am trying to write a regular expression to do a find and replace operation. Assume Java regex syntax. Below are examples of what I am trying to find:
12341+1
12241+1R1
100001+1R2
So, I am searching for a string beginning with one or more digits, followed by a "1+1" substring, followed by 0 or more characters. I have the following regex:
^(\d+)(1\\+1).*
This regex will successfully find the examples above, however, my goal is to replace the strings with everything before "1+1". So, 12341+1 would become 1234, and 12241+1R1 would become 1224. If I use the first grouped expression $1 to replace the pattern, I get the wrong result as follows:
12341+1 becomes 12341
12241+1R1 becomes 12241
100001+1R2 becomes 100001
Any ideas?
Your existing regex works fine, just that you are missing a \ before \d
String str = "100001+1R2";
str = str.replaceAll("^(\\d+)(1\\+1).*","$1");
Working link
IMHO, the regex is correct.
Perhaps you wrote it wrong in the code. If you want to code the regex ^(\d+)(1\+1).* in a string, you have to write something like String regex = "^(\\d+)(1\\+1).*".
Your output is the result of ^(\d+)(1+1).* replacement, as you miss some backslash in the string (e.g. "^(\\d+)(1\+1).*").
Your regex looks fine to me - I don't have access to java but in JavaScript the code..
"12341+1".replace(/(\d+)(1\+1)/g, "$1");
Returns 1234 as you'd expect. This works on a string with many 'codes' in too e.g.
"12341+1 54321+1".replace(/(\d+)(1\+1)/g, "$1");
gives 1234 5432.
Personally, I wouldn't use a Regex at all (it'd be like using a hammer on a thumbtack), I'd just create a substring from (Pseudocode)
stringName.substring(0, stringName.indexOf("1+1"))
But it looks like other posters have already mentioned the non-greedy operator.
In most Regex Syntaxes you can add a '?' after a '+' or '*' to indicate that you want it to match as little as possible before moving on in the pattern. (Thus: ^(\d+?)(1+1) matches any number of digits until it finds "1+1" and then, NOT INCLUDING the "1+1" it continues matching, whereas your original would see the 1 and match it as well).