So i got this regex code /[0-3][0-9][0-1][1-9]\d{2}[-\s]\d{4}?[^0-9]*|[0-3][0-9][0-1][1-9]\d{2}\d{4}/
This regex code take this kind of numbers:
1002821187
100282 1187
100282-1187
But i found out i dont want the numbers: 1002821187
So is it possible to make 1 regex code that only finds:
100282 1187
100282-1187
Your regex contains an alternation that matches the numbers with and without the space or -. You need to require that space or hyphen:
^[0-3][0-9][0-1][1-9][0-9]{2}[-\s][0-9]{4}$
^^^^^
See the regex demo. If you do not need to check for any boundaries, remove ^ and $ anchors that make the pattern match the whole string and use [0-3][0-9][0-1][1-9][0-9]{2}[-\s][0-9]{4}. Or use word boundaries to find whole words, \b[0-3][0-9][0-1][1-9][0-9]{2}[-\s][0-9]{4}\b.
Details
^ - start of string
[0-3] - a digit from 0 to 3
[0-9] - any digit
[0-1] (=[01]) - 0 or 1
[1-9] - any digit other than 0
[0-9]{2} - 2 digits
[-\s] - a - or whitespace
[0-9]{4} - 4 digits
$ - end of string.
Though you did not specify exactly what you are trying to do, here you can try the following regex :
\b\d{6}-\d{4}\b|\b\d{4}\b|\b\d{6}\b
Hope it helps.
Related
I am trying to validate a regex which
allows 10 digits
if digit starts with 672 then it should only allow total 9 digits
I have tried below regex
/^\d{10}$|^(672)\d{6}$/
https://regex101.com/r/0ahnKx/1
It works for 10 digits but if number starts with 672 then also it allows 10 digits.
Could anyone help how can I fix this?
Thanks!
First of all, the capturing group in your regex is redundant, it would make sense to wrap the part of the pattern between ^ and $ to only use single occurrences of the anchors.
To fix the issue, you need to make sure the first three digits matched by \d{10} are not 672, and you can achieve that with a negative lookahead:
/^((?!672)\d{10}|672\d{6})$/
/^(?:(?!672)\d{10}|672\d{6})$/
See the regex demo. Details:
^ - start of string
(?: - start of a group:
(?!672)\d{10} - no 672 substring check is triggered and then ten digits are matched
| - or
672\d{6} - 672 and six digits
) - end of the group
$ - end of string.
I created a Regex to check a string for the following situation:
first 4 chars are numbers
following by a point
following by 3 numbers
following by a point
following by 4 to 8 numbers or letters
ie: 1234.123.125B
My Regex: ^[0-9]{4}[.][0-9]{3}[.][0-9a-zA-Z]{4,8}$
But now I need a wildcard search: The Regex should also match if there is a '*' after the first 8 characters. For example:
1234.123.12* MATCH
1234.123* MATCH
1234.123.45B9* MATCH
1234.12* NO MATCH
1234.12345* NO MATCH
How can I add the wildcard search to my Regex?
Thank you
You may use this regex with alternation:
^\d{4}\.\d{3}(?:\*|\.[\da-zA-Z]{0,7}\*|\.[\da-zA-Z]{4,8})$
RegEx Demo
RegEx Details:
^: Start
\d{4}\.\d{3}: Match 4 digits + 1 dot + 3 digits
(?:\*|\.[\da-zA-Z]{0,7}\*|\.[\da-zA-Z]{4,8}): matches a single * OR a * after after a dot and 0 to 7 digits/letters OR match 4 to 8 digits/letters
$: End
My assumptions are that:
You don't allow wildcards to be mid-string
Nor do you want to allow wildcards after the full pattern (e.g.: 1234.123.12345678*).
So, alternatively you may possibily use something like:
^\d{4}\.\d{3}(?!.*\*.)(?![^*]{0,4}$)[.*][*\da-zA-Z]{0,8}$
See the online demo.
^ - Start string ancor.
\d{4}\.\d{3} - Four digits, a dot and another three digits.
(?!.*\*.) - Negative lookahead for zero or more characters followed by asterisk and another character other than newline.
(?![^*]{0,4}$) - Negative lookahead for zero to four characters other than asterisk before end string ancor.
[.*] - A literal dot or asterisk.
[*\da-zA-Z]{0,8} - Zero to eight characters from the character class.
$ - End string ancor.
I have a directive for input on angular app.
I´m building a regex for accepting:
1,2,3.2
1222,23
333333
Not accepting:
1,,333
132,.1
-123
I have to check char by char, which that I´m not putting a whole number in input. My try that not work.
^[,]{0,1}\d{0,10}[,]{0,1}([.]?\d{0,2})
In the most general case you may use
^\d+(?:,\d+)*(?:\.\d+)?$ // 432 or 4.653 or 1,44,43211 or 1,4456,43211754.876597
See the regex demo and the regex graph:
Details
^ - start of string
\d+ - 1+ digits
(?:,\d+)* - 0 or more repetitions of , and 1+ digits
(?:\.\d+)? - 1 or 0 repetitions of . and 1+ digits
$ - end of string.
Use limiting quantifiers and lookaheads to impose length restrictions. For example:
^(?!(?:,?\d){6})\d+(?:,\d+)*(?:\.\d{1,3})?$
^^^^^^^^^^^^^^^
The (?!(?:,?\d){6}) negative lookahead will fail the match if there are 6 or more digits in the integer part of the number. If you want to restrict the whole number length change the lookahead to (?!.{6}) and it will only match numbers up to 5 chars: 1, 1.55, 35.99 but not 100.12 (=6 chars).
I'm having issues with the validation of the chilean RUT/RUN with a regex expression in PCRE. I have the next regular expression but sadly can't make it work:
\b[0-9|.]{1,10}\-[K|k|0-9]
I need help to see what is wrong with the code. The application I need to use only uses PCRE.
Thank you.
You may use
^(\d{1,3}(?:\.\d{1,3}){2}-[\dkK])$
to match and capture (that is not usually necessary, but your app requires a capturing group to extract its contents) a whole string that matches the pattern. See the regex demo.
To match shorter strings that match this pattern inside a larger string, you may remove ^ and $ (see demo) or use \b word boundaries instead (see this demo).
Details:
^ - start of string
\d{1,3} - 1 to 3 digits
(?:\.\d{1,3}){2} - 2 sequences of a literal . and 1 to 3 digits
- - a hyphen
[\dkK] - a digit, k or K.
$ - end of string.
As they sometimes omit the dots, I used this one:
^(\d{1,2}(?:[\.]?\d{3}){2}-[\dkK])$
Details:
^ - start of string
\d{1,2} - 1 or 2 digits
(?:[.]?\d{3}){2} - 2 sequences of an optional '.' and 3 digits
- a hyphen
[\dkK] - a digit, k or K
$ - end of string
1234567-k OK
12345678-k OK
1.234.567-k OK
12.345.678-k OK
known issue:
12.345678-k and 12345.678-k still OK and I do not like this :(
You need to change to ^(\d{1,3}(?:\.\d{3}){2}-[\dkK])$ to capture only 2 sequence of 3 digits after the first sequence of 1-3 digits.
please consider being more specific in the REGEX build, since it matched wrong numbers, such as 17.87.335-2. Also the included one did't match formats without the dots or the hyphens.
Please consider using the following format: \b(\d{1,3}(?:(.?)\d{3}){2}(-?)[\dkK])\b
Modified prior version to try the other formats: https://regex101.com/r/2Us0j6/9
I need to match these values:
(First approach to a regex that roughly does what I want)
\d+([.,]\d{3})*[.,]\d{2}
like
24,56
24.56
1.234,56
1,234.56
1234,56
1234.56
but I need to not match
1.234.56
1,234,56
So somehow I need to check the last occurrence of "." or "," to not be the same as the previous "." or ",".
Background: Amounts shall be matched in English and German format with (optional) 1000-Separators.
But even with help of regex101 I completely fail at coming up with a correctly working look-behind. Any suggestions are highly appreciated.
UPDATE
Based on the answers I got so far, I came up with this (demo):
\d{1,3}(?:([\.,'])?\d{3})*(?!\1)[\.,\s]\d{2}
But it matches for example 1234.567,23 which is not desirable.
You may capture the digit grouping symbol and use a negative lookahead with a backreference to restrict the decimal separator:
^(?:\d+|\d{1,3}(?:([.,])\d{3})*)(?!\1)[.,]\d{2}$
^ ^ ^^^^^
See the regex demo
Group 1 will contain the last value of the digit grouping symbol and (?!\1)[.,] will match the other symbol.
Details:
^ - start of string
(?:\d+|\d{1,3}(?:([.,])\d{3})*) - either of the two alternatives:
\d+ - 1+ digits
| - or
\d{1,3} - 1 to 3 digits,
(?:([.,])\d{3})* - zero or more sequences of:
([.,]) - Group 1 capturing . or ,
\d{3} - 3 digits
(?!\1)[.,] - a . or , but not equal to what was last captured with ([.,]) pattern above
\d{2} - 2 digits
$ - end of string.
You can use
^\d+(([.,])\d{3})*(?!\2)[.,]\d{2}$
live demo