I am looking for help here. I want to write a regex to help me find EXACTLY a 7 digit in string - no more or less.
For instance in this string:
1234567 RE:TKT-2744870-R6P1G0: Gentle Reminder
It should return only 1234567
In this one:
12345678 RE:TKT-2744870-R6P1G0: Gentle Reminder
It should return none.
Can you help me with this one.
thanks in advance.
The proper regex should include \d{7} (7 digits) and 2 "border criteria",
for both start and end of the match, to block matching of a fragment
from longer sequence of digits.
My first thought was that neither before nor after the match there can be any digit.
But as I see from your example, these border criteria should be extended.
The set of "forbidden" chars (either before or after the match) should
include also - and letters.
E.g. 2744870 in your example data contains just 7 digits (no more, no less),
but you still don't want it to be matched, apparently because they are surrounded with - chars.
To keep the regex short, I propose:
(?<![\w-])\d{7}(?![\w-])
Details:
(?<![\w-]) - Negative lookbehind for word char or -.
\d{7} - 7 digits.
(?![\w-]) - Negative lookahead for word char or -.
If you decide to extend the set of "forbidden" chars in both border criteria,
just add them to [...] fragments in lookbehind / lookahead (but - char
should remain at the end, otherwise it must be quoted with \).
Regex like (\d{7})[^\d] (in other proposition) is wrong,
as it matches last 7 digits from any longer sequence of digits
(no "front border criterion").
It matches also both 2744870 (surronded with - chars), which are not
to be matched.
This one should do for your examples:
(\d{7})[^\d]
The first matching group contains the seven digits.
Alternatively –as suggested in the comments– you can use a negative lookahead to only match the seven digits and not require matching groups:
^\d{7}(?!\d)
Related
The regular expression that I am trying to create should match all numbers that contain three '8's in any 4 digit number. The regular expression that I have only matches the first 10 numbers out of the list of 15 numbers. Any suggestions will be greatly appreciated.
\b[0-9]*(?:8[0-9]*[0-9]?8|8[0-9]*[0-9]?8|8[0-9]*[0-9]?8)\b
Test data:
8088 8188 8288 8388 8488 8808 8818 8828 8838 8848 8880 8881 8882 8883 8884
The last five numbers should also match, but don't.
You can use
\b(?=\d{4}\b)(?:[0-79]*8){3}[0-79]*\b
See the regex demo.
Details:
\b - a word boundary
(?=\d{4}\b) - there must be 4 digits immediately on the right and they should be followed with a word boundary
(?:[0-79]*8){3} - three occurrences of any 0 or more digits but 8 and then 8
[0-79]* - any 0 or more digits but 8
\b - word boundary.
If it's guaranteed that the number is a four-digit number, then you can try the following:
\b8*[0-79]8*\b
To analyze what each part matches, you can check using,
\b(8*)[0-79](8*)\b
This should do it. This will match any of the 4 patterns.
([\d888]|[8\d88]|[88\d8]|[888\d])
You may want to add a check for the delimiter (in your example the space) as this pattern will match across the spaces giving you many more results
\b(\d?8{3}\d?)\b
this makes the first and last digit in the word bound optional, use
either ? or {0,1}
add quantifier to your eight to have exactly
number of eights you need {3}
replace [0-9] with \d as
Digit for brewity
supposed you have only numbers of length 4. Otherwise use an alternative without optional digits: \b(\d8{3}|8{3}\d)\b
I've run into some issues with this one and cannot find it in past questions.
Criteria:
Reject pure digits
Reject pure letters
Reject any symbols
Accept ONLY Alphanumeric combo
MUST be equal to 10 characters total
Here is what I have made and the problems with each:
^(?!^\d*$)[a-zA-Z\d]{10}$
This fails criteria #2
^[a-zA-Z0-9]{10}$
This fails criteria #1
I have tried some others that meet all criteria but fail the 10 char limit.
Any help is appreciated.
You may use a second lookahead:
^(?!\d+$)(?![a-zA-Z]+$)[a-zA-Z\d]{10}$
See the regex demo and the Regulex graph:
Details
^ - start of string
(?!\d+$) - a negative lookahead that makes sure the whole string is not composed of just digits
(?![a-zA-Z]+$) - the whole string cannot be all letters
[a-zA-Z\d]{10} - 10 letters or digits
$ - end of string.
Try this:
(?=^.{10}$)^([a-z]+\d[a-z0-9]*|\d+[a-z][a-z0-9]*)$
Demo
Explanation:
(?=^.{10}$)^([a-z]+\d[a-z0-9]*|\d+[a-z][a-z0-9]*)$
(?=^.{10}$) # there's exactly 10 characters following
^( | )$ # we match the entire string, containing either:
[a-z]+\d[a-z0-9]* # letters, followed by a number, followed by alphanumerics, or
\d+[a-z][a-z0-9]* # numbers, followed by a letter, followed by alphanumerics
Use lookahead to find at least one char of each type you require, and specify the length and char limitation in the "regular" part of your regex:
^(?=.*[a-zA-Z])(?=.*\d)[0-9a-zA-Z]{10}$
(?=.*[a-zA-Z])- Look ahead and find a letter,
(?=.*\d) - Look ahead and find a digit
[0-9a-zA-Z]{10} - exactly 10 digit/letter chars
First off, this has sort of been asked before. However I haven't been able to modify this to fit my requirement.
In short: I want a regex that matches an expression if and only if it only contains digits, and there are 5 (or more) increasing consecutive digits somewhere in the expression.
I understand the logic of
^(?=\d{5}$)1*2*3*4*5*6*7*8*9*0*$
however, this limits the expression to 5 digits. I want there to be able to be digits before and after the expression. So 1111345671111 should match, while 11111 shouldn't.
I thought this might work:
^[0-9]*(?=\d{5}0*1*2*3*4*5*6*7*8*9*)[0-9]*$
which I interpret as:
^$: The entire expression must only contain what's between these 2 symbols
[0-9]*: Any digits between 0-9, 0 or more times followed by:
(?=\d{5}0*1*2*3*4*5*6*7*8*9*): A part where at least 5 increasing digits are found followed by:
[0-9]*: Any digits between 0-9, 0 or more times.
However this regex is incorrect, as for example 11111 matches. How can I solve this problem using a regex? So examples of expressions to match:
00001459000
12345
This shouldn't match:
abc12345
9871234444
While this problem can be solved using pure regular expressions (the set of strictly ascending five-digit strings is finite, so you could just enumerate all of them), it's not a good fit for regexes.
That said, here's how I'd do it if I had to:
^\d*(?=\d{5}(\d*)$)0?1?2?3?4?5?6?7?8?9?\1$
Core idea: 0?1?2?3?4?5?6?7?8?9? matches an ascending numeric substring, but it doesn't restrict its length. Every single part is optional, so it can match anything from "" (empty string) to the full "0123456789".
We can force it to match exactly 5 characters by combining a look-ahead of five digits and an arbitrary suffix (which we capture) and a backreference \1 (which must exactly the suffix matched by the look-ahead, ensuring we've now walked ahead 5 characters in the string).
Live demo: https://regex101.com/r/03rJET/3
(By the way, your explanation of (?=\d{5}0*1*2*3*4*5*6*7*8*9*) is incorrect: It looks ahead to match exactly 5 digits, followed by 0 or more occurrences of 0, followed by 0 or more occurrences of 1, etc.)
Because the starting position of the increasing digits isn't known in advance, and the consecutive increasing digits don't end at the end of the string, the linked answer's concise pattern won't work here. I don't think this is possible without being repetitive; alternate between all possibilities of increasing digits. A 0 must be followed by [1-9]. (0(?=[1-9])) A 1 must be followed by [2-9]. A 2 must be followed by [3-9], and so on. Alternate between these possibilities in a group, and repeat that group four times, and then match any digit after that (the lookahead in the last repeated digit in the previous group will ensure that this 5th digit is in sequence as well).
First lookahead for digits followed by the end of the string, then match the alternations described above, followed by one or more digits:
^(?=\d+$)\d*?(?:0(?=[1-9])|1(?=[2-9])|2(?=[3-9])|3(?=[4-9])|4(?=[5-9])|5(?=[6-9])|6(?=[7-9])|7(?=[89])|8(?=9)){4}\d+
Separated out for better readability:
^(?=\d+$)\d*?
(?:
0(?=[1-9])|
1(?=[2-9])|
2(?=[3-9])|
3(?=[4-9])|
4(?=[5-9])|
5(?=[6-9])|
6(?=[7-9])|
7(?=[89])|
8(?=9)
){4}
\d+
The lazy quantifier in the first line there \d*? isn't necessary, but it makes the pattern a bit more efficient (otherwise it initially greedily matches the whole string, requiring lots of failing alternations and backtracking until at least 5 characters before the end of the string)
https://regex101.com/r/03rJET/2
It's ugly, but it works.
I would like to match the following pattern
1.XXXXXX.XXX.X
The combination must begin with a 1 and must contain at least a second number greater than 0 somewhere. Only numbers and points allowed.
So the following examples would be correct
1.000000.000.1
1.000500.000.0
1.020030.030.0
And the following examples would be incorrect
1.000000.000.0
1.0000.00.0
1.0000d0.020.0
What I have created so far
(?=^[1][\.][0-9]{6}[\.][0-9]{3}[\.][0-9]{1}$) // check pattern 1.XXXXXX.XXX.X
(?=^[1-9](?!0000000000$)[0-9][1-9]?\d+$) // check if input is greater than 10000000000
Unfortunately, the second statement does not work because of the points in the input. Is it possible to read the complete number and ignore the points or is there a better solution?
You may use
^1\.(?!(?:\.?0)+$)\d{6}\.\d{3}\.\d$
See the regex demo
The regex will fail the match if there are only zeros and dots after the initial 1..
Details
^1\. - 1. at the start of the string
(?!(?:\.?0)+$) - a negative lookahead that will fail the match of there are one or more sequences of an optional . and a zero up to the end of the string
\d{6} - 6 digits
\. - a dot
\d{3} - 3 digits
\. - a dot
\d - a digit
$ - end of string.
I am trying to match an exactly 8 digit phone number that has 0 or more dashes in it. For example, the following should all match:
12345678
123456-78
1234-5678
1-2-3-4-5-6-7-8
If I ignore the dashes, it is rather simple. I can just use:
[\d]{8}
If I want to match a string containing at least 8 characters (digits and dashes) I can use:
[\d-]{8,}
However, here I can't put an upper bound on the number of characters because I don't know how many dashes the number would have.
The only way I thought of would be to use:
[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9][-]?[0-9]
However, this seems really messy for something that (at least in my mind) seems simple. Is there an easier way to do this?
You can use this regex with optional - after each digit:
^([0-9]-?){8}$
If your regex supports \d then use:
^(\d-?){8}$
RegEx Demo
You should use
^[0-9](-?[0-9]){7}$
^([0-9]-?){8}\b$
See the regex demo #1 and regex demo #2, where \b is used to make sure the last char is a digit (that is a word char).
Details
^ - start of string
[0-9] to match a digit since \d in various regex flavors may match more than just ASCII digits from 0 to 9.
(-?[0-9]){7} - matches 7 sequences of an optional hyphen and a digit, and will not allow trailing hyphen at the end of the string.
([0-9]-?){8} - matches eight occurrences of a digit followed with an optional - char
\b$ - is a trick to make sure the last char is of a word type. Since the pattern can only match a - (a non-word char) or a digit at the end, \b automatically makes sure the last char is a digit.