I'm trying to validate french mobile numbers:
I have already removed all non numeric character and the eventual 00 at beginning, and rules are:
start with 06 or 07 or 09
is 10 digit long:
thus :
/^0(6|7|9)\d{8}$/
but (seems) that if countrycode (33) is present, the leading zero has to be avoided, but at this point I cannot create the right regex, since with number:
33614444444
/^(33|0)?(6|7|9)\d{8}$/
it works, but works also with
614444444
while it should not
can suggest solution?
you can do it using the regex
^(33|0)(6|7|9)\d{8}$
see the regex101 demo
Why don't you simply use /^(33|0)(6|7|9)\d{8}$/ ?
I do not think you need the quantifier ?.
When you add ? after (33|0). It implies either none of them is present or one of 33 or 0 is present. It would match all the following -
614444444 // none present
0614444444 // 0 present
33614444444 // 33 present
Related
Given the string below,
ay bee ceefooh deefoo38 ee 37 ef gee38 aitch 38 eye19 jay38 kay 99 el88 em38 en 29 ou38 38 pee 12 q38 arr 999 esss 555
the goal is to match every word such that the suffix is a number that matches the number that appears after foo (which happens to be 38 in this case).
There is only one substring that begins with foo and ends with a number. The expected matches all exist after said substring.
Expected matches:
gee38
jay38
em38
ou38
q38
I've tried foo(\d+).*?(\w+\1)\b and foo(\d+).*(\w+\1)\b, but they fail to match all, because they either match the first one (gee38) or the last one (q38).
Is it possible to match all with just a single regex and, importantly, in just a single run?
The PCRE2 engine that I use behaves in the same way as https://regex101.com/r/uFEDOE/1. So, if the regex can match multiple substrings on regex101, then the engine that I use can too.
(?:foo|\G(?!^))(\d+).*?(?=(\w+))\w+(?=\1\b)
Demo
It could be some size or performance optimization.
#Niko Gambt, say if any optimization is important for you.
I have a hexadecimal string with space separator for each byte.
eg., A1 B2 C3 D4 E5 FF 00 11 22 33 44 ...
I would like to use a regex validator to verify the user input is correct or not?
How could I write the regular expression to achieve this goal?
Something like this:
^[A-F0-9]{2}( [A-F0-9]{2})*$
Explanation:
^ - anchor: string start
[A-F0-9]{2} - two symbols in either 0..9 or A..F range
( [A-F0-9]{2})* - followed by space and two 0..9 or A..F symbols zero or more times
$ - anchor: string end
If you allow a..f as valid hexadecimal symbols
^[A-Fa-f0-9]{2}( [A-Fa-f0-9]{2})*$
I would like to propose a solution based on DRY principle
(Don't Repeat Yourself).
Instead of writing the same pattern (as Dmitry proposed), you can:
Write the pattern for 2 hex digits as a capturing group - ([A-F0-9]{2}).
"Call" it again using (?1).
So the whole pattern can be ^([A-F0-9]{2})( (?1))*$.
There are also other variants of "calling" a capturing group, e.g.
(?-1) - call the preceding group or
(?&name) - call a named group.
For details see https://www.regular-expressions.info/subroutine.html
I have a regex ".*00$" which restricts that the input must be ending with 00. How can I improve this to add a max length of 20 also. So:
100 => valid,
00 = > invalid,
12345678900 => valid,
111111111111111111100 = > 21 digits - invalid.
Based on your title and short description this should probably work for you:
^[0-9]{1,18}00$
This will allow 3-20 digits in input that ends with 00
This can be expressed almost literally as
/^[0-9]{1,18}00$/
20 digits, the last two being zeros.
You can use {} to specify a range of characters, so
".{1,18}00"
would allow any 1-18 characters followed by 00. If you want to restrict it further, you could use
"[1-9][0-9]{0,17}00"
so that you ensure the first number is not 0, followed by 0-17 numbers and finally 00.
Try this :
^[1-9][0-9]{0,17}00$
The first [] will ensure it doesn't start by 0.
I'm new to Regular expressions and working on something that will return all UK phone numbers with an area code beginning 01, 02, 03 or 07 only. It has to not look up 08 or 09. It also has to take in to account the different grouping styles too. But here's the kicker... it's got to be 80 characters or less.
This was my best shot:
(01|02|03|07|44\D*1|44\D*2|44\D*3|44\D*7|)(\d\D*){9}
The problem is that it's returning any 9 digit or less number and I can't figure out why.
Any help would be grand!
(01|02|03|07|44\D*1|44\D*2|44\D*3|44\D*7) is matching either 0 or 44\D* followed by 1, 2, 3 or 7 which simplifies to:
(?:44\D*|0)[1237]
Putting that with the rest gives:
(?:44\D*|0)[1237](\D*\d\D*){9}
Debuggex Demo
i have a unique challenge.
i want to create a google analytics filter for a custom variable that only returns a value if the given string is smaller or equal than '001700'. yeah, i know that a string can't be smaller, still i need to find a way to make this work.
oh, and if you ask: no there is no way to convert that string to a number (according to my knowledge - via a google analytics filter - and that is what i have to work with in this case).
so basically, i have
000000
000001
000002
000003
...
...
999998
999999
and i need a regular expression that matches
001700
001699
001698
...
...
000001
000000
but does not match
001701
001702
...
...
999998
999999
sub question a) is it possible? (as i have learned, everything is possible with regExp if you are clever and/or masochistic enough)
sub question b) how to do it?
thx very much
You can do:
^00(1700|1[0-6][0-9]{2}|0[0-9]{3})$
See it
yes you can do
see this article
Eg:
alert('your numericle string'.replace(/\d+/g, function(match) {
return parseInt(match,10) <= 17000 ? '*' : match;
}));
JavaScript calls our function, passing
the match into our match argument.
Then, we return either the asterisk
(if the number matched is under 17000) or
the match itself (i.e. no match should
take place).
Can be done with RegEx:
/00(1([0-6][0-9]{2}|700)|0[0-9]{3})/
Explanation:
00 followed by
1 followed by 0 to 6 and any 2 numbers = 1000 - 1699
or
1700
or
0 followed by any 3 numbers = 0000 - 0999