I need a regex which matches with 1 to 6 length digits which is easy \d{1,6}, however I need avoid if there is longer number than 6 digits.
For example, it should match 233 in sentence, my id is 233, but it should not match my id is 222334444
It should only match if there is space before first digit of number.
Thanks
Its hard to tell what the expected character is on either side.
Generally, it could be done using word boundary \b\d{1,6}\b
Use lookarounds.
(?<=^|\s)\d{1,6}(?!\d)
How about?
Online Demo
(?<!\d)\d{1,6}$
Related
I am trying to match all characters between "xAA" and "xFF" but only if theres at least 4 characters between those. Is there a simple way to do this with RegEx?
For example, xAA-12345-xFF should be matched but xAA-1-xFF should be ignored.
My RegEx currently looks like this:
"(?<=\xAA).*(?=\xFF)"
this does match everything between those characters, but i can't find a way on how to only match if at least 4 characters between them, can someone help me?
Quick and dirty hack could be:
"xAA....*xFF"
This requires three (...) characters followed by 0 or more (.*).
Do this:
xAA\S{4,}xFF
\S{4,} matches at least 4 non whitespace characters.
Demo
(?<=xAA-)[0-9]{4,}(?=\-xFF)
retracts 12345 from xAA-12345-xFF in case there are only digits
(?<=xAA-)[0-9a-zA-Z]{4,}(?=\-xFF) if letters or digits are possible
regex101.com
I am about to build a regex pattern to extract a number from a string which is unknown and can be different every time..
Because it is always unknown how my string looks, here a some common examples:
12cm iamtext 311
iamtext 311 12 cm iamtext 311
iamtext 311 12cm
Summed up: What I am aiming for is the number before cm or cm (space). This pattern can show up with a undefined amount of numbers. So, it could also be something like 12414 cm. In this case I want to get the 12414.
But if there is something like iamtext311 cm I don't want to get anything back cause in this case the number belongs to the text. But if there is a space between the number and the text, I want to get the 311.
This is what I got so far:
.*?\d+.*?(\d+)
But this isn't working for chars.. and I don't know how to process at the moment.. Cause it is such a complex situation especially with all the different cases with and without a space...
Would appreciate any kind of help!
How about that with \b with optional space character?
\b\d+\s?cm\b
DEMO: https://regex101.com/r/fsp3FS/10
Split the problem.
The number is obtained with the obvious \d+.
You don't want it preceded by any character but spacing characters: (?<!\S).
Must be followed by an optional space then characters cm: (?=\s?cm).
Put it together: (?<!\S)\d+(?=\s?cm).
Demo.
In your pattern .*?\d+.*?(\d+) you don't account for the cm part.
What you might do instead is assert the start of the string or match 1+ times a whitespace character and use a capturing group for the digits.
To prevent cm to be part of a longer word, you could add a word boundary \b:
(?:^|\s+)(\d+) ?cm\b
regex101 demo
If you don't want to match newlines using \s+ you could use a character class to match a space and/or a tab [ \t]
I have searched around quite a bit, but I haven't found a solution for my problem yet.
I'm trying to create a regex that will allow me to match the following examples:
YOUU 410831 0
MEIU 810851 0
I got to \b(YOUU|MEIU)\w*\b.
But then I can't seem to add a space, then a number, then a space again, and finally a digit. How could I achieve this?
You are looking for something like
[A-Z]+(?:\s+[0-9]+)+
See demo
Or, if there are 2 set groups of numbers after the word, and the 1st number is 6 digits in size, and the last digit is always of size 1:
[A-Z]+\s+[0-9]{6}\s+[0-9]\b
Demo 2
With i option, the words with lowercase letters will also be matched.
Try to use this:
\b(YOUU|MEIU) \d+ \d\b
REGEX DEMO
You are probably looking for this regex?
\b(YOUU|MEIU)\s+\d+\b\s+\d
if the numbers in the middle are always 6 numbers, you may want to fix that with
\b(YOUU|MEIU)\s+\d{6}\b\s+\d
I'm looking to build a regex that matches the following group of numbers:
10xxxxxxx
1116xxxxx
143xxxxxx
146xxxxxx
149xxxxxx
159xxxxxx
16xxxxxxx
(note the length is always 9)
where x is any digit. My best attempt yielded this:
/^1[01456][1369]*[6]*[0-9]$/
However, I can't get the length of the string to always be 9. Any ideas?
Edit: Maybe I wasn't clear enough, it needs to match those 7 cases, and ONLY those, inclusively and exclusively.
How about:
^1(?:[06]\d{2}|116|4[369]\d|59\d)\d{5}$
use this pattern
^1[01456](16|3\d|6\d|9\d|\d\d)\d{5}$
Is this what you want?
^(?=[0-9]{9}$)(?:10|1116|143|146|149|159|16)
Demo
This starts by looking at the beginning of the string for exactly 9 digits using a positive lookahead anchored to the end of the string. Then we look for any of your 7 specific groups of numbers that the string can start with.
You can use this regex:
/^1[01456][1369][0-9]{6}$/
Since 3 digits are already matched by first 3 patterns 1, [01456] and [1369] so last one must match exact 6 characters to enforce it a 9 digit input.
I am trying to create a regex to have a string only contain 0-9 as the characters and it must be at least 1 char in length and no more than 45. so example would be 00303039 would be a match, and 039330a29 would not.
So far this is what I have but I am not sure that it is correct
[0-9]{1,45}
I have also tried
^[0-9]{45}*$
but that does not seem to work either. I am not very familiar with regex so any help would be great. Thanks!
You are almost there, all you need is start anchor (^) and end anchor ($):
^[0-9]{1,45}$
\d is short for the character class [0-9]. You can use that as:
^\d{1,45}$
The anchors force the pattern to match entire input, not just a part of it.
Your regex [0-9]{1,45} looks for 1 to 45 digits, so string like foo1 also get matched as it contains 1.
^[0-9]{1,45} looks for 1 to 45 digits but these digits must be at the beginning of the input. It matches 123 but also 123foo
[0-9]{1,45}$ looks for 1 to 45 digits but these digits must be at the end of the input. It matches 123 but also foo123
^[0-9]{1,45}$ looks for 1 to 45 digits but these digits must be both at the start and at the end of the input, effectively it should be entire input.
The first matches any number of digits within your string (allows other characters too, i.e.: "039330a29"). The second allows only 45 digits (and not less). So just take the better from both:
^\d{1,45}$
where \d is the same like [0-9].
Use this regular expression if you don't want to start with zero:
^[1-9]([0-9]{1,45}$)
If you don't mind starting with zero, use:
^[0-9]{1,45}$
codaddict has provided the right answer. As for what you've tried, I'll explain why they don't make the cut:
[0-9]{1,45} is almost there, however it matches a 1-to-45-digit string even if it occurs within another longer string containing other characters. Hence you need ^ and $ to restrict it to an exact match.
^[0-9]{45}*$ matches an exactly-45-digit string, repeated 0 or any number of times (*). That means the length of the string can only be 0 or a multiple of 45 (90, 135, 180...).
A combination of both attempts is probably what you need:
^[0-9]{1,45}$
^[0-9]{1,45}$ is correct.
Rails doesnt like the using of ^ and $ for some security reasons , probably its better to use \A and \z to set the beginning and the end of the string
For this case word boundary (\b) can also be used instead of start anchor (^) and end anchor ($):
\b\d{1,45}\b
\b is a position between \w and \W (non-word char), or at the beginning or end of a string.