I have searched around and cannot find an answer to my problem.
I have a range of numbers that I need to match against, it can be any one of these numbers but it must be a full match on a number not partially matched, see below:
Numbers to match against:
4 46 64
Now if I have the number 48, the regular expression should fail as the number 48 does not exist. My regex seems to matching on all of the number 4's for example and that is my issue.
Regular Expression:
/4|46|64/g
The text to match against is:
48
Result:
0-1: 4
See my example on an online regex tester:
https://regex101.com/r/QPUsqa/1
Thanks in advance.
Just add the anchors ^ and $ to modify it to:
^(?:4|46|64)$
Click for Demo
Explanation:
^ - asserts the start of the string
(?:4|46|64) - matches either 4 or 46 or 64
$ - asserts the end of the string
Related
Hello I need to exclude sequence of digits from 890000 till 890001;
890002 to 899999 is acceptable
Is it possible doing using regular expression?
No need for regex.
If Value >= 890002 And Value <= 899999 Then
' Accept
End If
Ok, if you insist on using regex (may be for learning purpose):
In this simple case it is actually easier to exclude those two number and match the rest:
^89(?!000[12])\d{4}$
Explanation:
^ match from start of text
89match 89
(?!000[12]) negative look ahead for 3 times zero and one of characters in the character group (1 or 2). If this doesn't block the match:
\d{4} match 4 digits
$ match end of text.
I have files with these filename:
ZATR0008_2018.pdf
ZATR0018_2018.pdf
ZATR0218_2018.pdf
Where the 4 digits after ZATR is the issue number of magazine.
With this regex:
([1-9][0-9]*)(?=_\d)
I can extract 8, 18 or 218 but I would like to keep minimum 2 digits and max 3 digits so the result should be 08, 18 and 218.
How is possible to do that?
You may use
0*(\d{2,3})_\d
and grab Group 1 value. See the regex demo.
Details
0* - zero or more 0 chars
(\d{2,3}) - Group 1: two or three digits
_\d - a _ followed with a digit.
Here is a PCRE variation that grabs the value you need into a whole match:
0*\K\d{2,3}(?=_\d)
See another regex demo
Here, \K makes the regex engine omit the text matched so far (zeros) and then matches 2 to 3 digits that are followed with _ and a digit.
(?:[1-9][0-9]?)?[0-9]{2}(?=_[0-9])
or perhaps:
(?:[1-9][0-9]+|[0-9]{2})(?=_[0-9])
(https://www.freeformatter.com/regex-tester.html, which claims to use the XRegExp library, that you mention in another answer doesn't seem to backtrack into the (?:)? in my first suggestion where necessary, which makes it very different from any regex engine I've encoutered before and makes it prefer to match just the 18 of 218 even though it starts later in the string. But it does work with my second suggestion.
([1-9]\d{2,3})(?=_\d)
{x,y} will match from x to y times the previous pattern, in this case \d
Edit: from your own regex it looked as you wanted the part of the number which starts with a non-zero. However since your examples include leading 0s, maybe you really wanted :
(\d{2,3})(?=_\d)
Which will give you the last 3 digits before underscore unless there are only 2 digits.
I propose you:
^ZATR0*(\d{2,3})_\d+\.pdf$
demo code here. Result:
Match 1 Full match 0-17 ZATR0008_2018.pdf Group 1. 6-8 08
Match 2 Full match 18-35 ZATR0018_2018.pdf Group 1. 24-26 18
Match 3 Full match 36-53 ZATR0218_2018.pdf Group 1. 41-44 218
I'm trying to build a regular expression to match exactly this kind of format:
40XXXXXX
41XXXXXX
43XXXXXX
So far i have this \d{2}(^40?|^41?|^43?)\d{6}
But it doesn't work, i've spend hours in http://regexr.com/ trying to make it work without luck
Appreciate your help
Use following regex
/^4(0|1|3)[0-9]{6}$/
Regex Demo
Or,
/^4[013][0-9]{6}$/
Demo
Explanation:
^4: Start with 4
(0|1|3): Matches 0 or 1 or 3. | is OR in regex
[013]: Matches any one digit from the character class
[0-9]{6}: Matches any number from 0 to 9 exactly six times
Hi I need a regular expression for phone number looking like +NNNNN, where + is optional and NNNN are arabic numbers only (from 0 to 9).
The phone number must be between 3 and 20 symbols. I use the following expression [+]{0,1}[0-9]{3,20}$ but it is not working correctly.
Where am I making a mistake?
You didn't anchor the start of your pattern with ^ but you did it on the end with $
This should work:
^\+?[0-9]{3,20}$
Just add anchor at the begining of the regex:
^[+]?[0-9]{3,20}$
^[+]{0,1}[0-9]{3,20}$
should work, you have to use ^ to match from the beginning
I am facing a problem to extract a particular numeric value from an input string.
Say, I had one string "this23 is 67 test 56 string 45", and i have to fetch number 67 from the string then how can i extract this using regular expresseion?
I only have to use regular expression.
Do you have any idea about it?
Thanks in advance,
[^\d]*[\d]+[^\d]+([\d]+)
The first chunk is 0 or more non-digits, followed by 1 or more digits, followed by 1 or more non-digits, followed by 1 or more digits. In parentheses so that you can capture it in whatever language you're using.
ignore any non-digit
ignore any digit (first number)
again ignore any non-digit
capture the second number
[\D]*[\d]+[\D]+([\d]+)
try this:
^\D*\d+\D+(\d+)
I test this using Rubular -> the 67 is in the first group.
It is possible write a Regular expression with a look-behind command (?<=expression) but it is not possible to use * or + inside (at least in Ruby and Perl it is not possible).