I am looking for a regular expression that can be used to find numbers that begin and end with different digits.
I tried with the following: ^(\d)\d*(?!\1)$
However, this does not work, it gives positive matches for numbers like
121
1233
1441
Where am I getting it wrong? Any ideas?
You could use the positive lookahead (?!\d*\1$) after the capturing group to assert that what follows is not zero or more times a digit ending with group 1:
^(\d)(?!\d*\1$)\d*$
Your regex doesn't actually match the last digit. You should do:
^(\d)\d*(?!\1)\d$
^^
match the last digit!
Your regex just asserts that there isn't the starting digit at the end. Well, an empty string is also "not the starting digit", so it matches things like 1221. You have to tell it to match "a digit that is not the starting digit".
Demo
Related
I should only catch numbers which are fit the rules.
Rules:
it should be 16 digit
first 11 digit can be any number
after 3 digit should have all zero
last two digit can be any number.
I did this way;
([0-9]{11}[0]{3}[0-9]{2})
number example:
1234567890100012
now I want to get the number even it has got any letter beginning or ending of the string like " abc1234567890100012abc"
my output should be just number like "1234567890100012"
When I add [a-zA-Z]* it gives all string.
Also another point is if there is any number beginning or ending of the string like "999912345678901000129999". program shouldn't take this. I mean It should return none or nothing. How can I write this with regex.
You can use look around to exclude the cases where there are more digits before/after:
(?<!\d)\d{11}000\d\d(?!\d)
On regex101
You can use a capture group, and match optional chars a-zA-Z before and after the group.
To prevent a partial match, you can use word boundaries \b or if the string should match from the start and end of the line you can use anchors ^ and $
\b[a-zA-Z]*([0-9]{11}000[0-9]{2})[a-zA-Z]*\b
Regex demo
I am trying to detect if a string has a number based on few conditions. For example I don't want to match the number if it's surrounded by parentheses and I'm using the lookahead and lookbehind to do this but I'm running into issues when the number contains multiple digits. Also, the number can be between text without any space separators.
My regex:
(?https://regex101.com/r/RnTSMJ/1
Sample examples:
{2}: Should NOT Match. //My regex Works
{34: Should NOT Match. //My regex matches 4 in {34
45}: Should NOT Match. //My regex matches 4 in {45
{123}: Should NOT Match. //My regex matches 2 in {123}
I looked at Regex.Match whole words but this approach doesn't work for me. If I use word boundaries, the above cases work as expected but then cases like the below don't where numbers are surrounded with text. I also want to add some additional logic like don't match specific strings like 1st, 2nd, etc or #1, #2, etc
updated regex:
(?<!\[|\{|\(|#)(\b\d+\b)(?!\]|\}|\|st|nd|rd|th)
See here https://regex101.com/r/DhE3K4/4
123abd //should match 123
abc345 //should match 234
ab2123cd // should match 2123
Is this possible with pure regex or do I need something more comprehensive?
You could match 1 or more digits asserting what is on the left and right is not {, } or a digit or any of the other options to the right
(?<![{\d#])\d+(?![\d}]|st|nd|rd|th)
Explanation
(?<![{\d#]) Negative lookbehind, assert what is on the left is not {, # or a digit
\d+ Match 1+ digits
(?! Negative lookahead, assert what is on the right is not
[\d}]|st|nd|rd|th Match a digit, } or any of the alternatives
) Close lookahead
Regex demo
The following regex is giving the expected result.
(?<![#\d{])\d+(?!\w*(?:}|(?:st|th|rd|nd)\b))
Regex Link
I want to remove the zeros at the end of a number coming after the decimal point. To give an example:
12.009000 should match "000"
I have the regex pattern below but it gives an error A quantifier inside a lookbehind makes it non-fixed width and I can't find any solution to fix that. What is the correct pattern to match successfully?
Pattern: (?<=\.[0-9]*)0+$
With Java, you can do it like this.
(\\d) capture digits
followed by 0's
replace with the captured digits.
$1 is the back reference to the capture group
str = str.replaceAll("(\\.\\d+?)0+$","$1");
System.out.println(str);
Note: It will leave 12.000000 as 12.0.
(\d+[.]?\d*?)0*$
One more step is needed to replace the dot for numbers such as 12.000
Click here for demo: Click Here
Or to deal with numbers such as 12.000 in one step:
(?:(\d+)\.0*$)|(?:(\d+[.]?\d*?)0*$)
Click here for demo: Click Here
Here is my attempt:
(?:[.][0-9]*[1-9])(0+)$|([.]0+$)
This assumes that the input string is actually a number (it won't protect against things like xyz.001). It will not match at all if there are no trailing zeros after decimal point; and if there are, it removes:
sequence of 0s preceded by a [1-9] after [.][0-9]*
or
a [.] followed by a sequence of 0s.
The result will always be in the captured group if the regex matches.
([\d.]+?)(0*)
"Find digits and dots, but not greedily, then find trailing zeros"
Group 1 is the number. Group 2 is the trailing zeros.
I am trying to write a regular expression that will match a two digit number where the two digits are not same.
I have used the following expression:
^([0-9])(?!\1)$
However, both the strings "11" and "12" are not matching. I thought "12" would match. Can anyone please tell me where I am going wrong?
You need to allow matching 2 digits. Your regex ^([0-9])(?!\1)$ only allows 1 digit string. Note that a lookahead does not consume characters, it only checks for presence or absence of something after the current position.
Use
^(\d)(?!\1)\d$
^^
See demo
Explanation of the pattern:
^ - start of string
(\d) - match and capture into Group #1 a digit
(?!\1) - make sure the next character is not the same digit as in Group 1
\d - one digit
$ - end of string.
I've got a regex issue, I'm trying to ignore just the number '41', I want 4, 1, 14 etc to all match.
I've got this [^\b41\b] which is effectively what I want but this also ignores all single iterations of the values 1 and 4.
As an example, this matches "41", but I want it to NOT match:
\b41\b
Try something like:
\b(?!41\b)(\d+)
The (?!...) construct is a negative lookahead so this means: find a word boundary that is not followed by "41" and capture a sequence of digits after it.
You could use a negative look-ahead assertion to exclude 41:
/\b(?!41\b)\d+\b/
This regular expression is to be interpreted as: At any word boundary \b, if it is not followed by 41\b ((?!41\b)), match one or more digits that are followed by a word boundary.
Or the same with a negative look-behind assertion:
/\b\d+\b(?<!\b41)/
This regular expression is to be interpreted as: Match one or more digits that are surrounded by word boundaries, but only if the substring at the end of the match is not preceded by \b41 ((?<!\b41)).
Or can even use just basic syntax:
/\b(\d|[0-35-9]\d|\d[02-9]|\d{3,})\b/
This matches only sequences of digits surrounded by word boundaries of either:
one single digit
two digits that do not have a 4 at the first position or not a 1 at the second position
three or more digits
This is similar to the question "Regular expression that doesn’t contain certain string", so I'll repeat my answer from there:
^((?!41).)*$
This will work for an arbitrary string, not just 41. See my response there for an explanation.