In my Google form I would like to authorize numbers and range of numbers like this:
1 to 9,
10 to 80,
90,
100
The separator can be , | ; or newline character, other examples
100
110 to 115
540
or
50 | 60 | 70 to 80 | 100
I was expecting this regEx to work (selecting Regular Expression > Matches > (to)|[0-9 \n|,;]*) but it failed.
Any idea ?
You can use
^\d+(?:(?:\s*(?:to|[\n|;,])\s*)\d+)*\n*$
See the regex demo. Details:
^ - start of a string
\d+ - one or more digits
(?:\s*(?:to|[|;,])\s*\d+)* - zero or more occurrences of
\s*(?:to|[\n|;,])\s* - to or |, ;, , or newline enclosed with zero or more whitespaces
\d+ - one or more digits
\n* - zero or more newlines
$ - end of string.
Related
I need a RegEx for a numeric value that starts at 60.00 and has no upper limit.
I have used the below RegEx for numbers between 0.00 and 60.00, but now I need a similar expression for all numbers over 60.00 with 2 decimal places
^(?=.)(?:(?:(?:0|[1-5]?\d)?(?:[,.]\d{1,2})?)|60(?:[.,]00?)?)$
You can use
^(?!60(?:\.00?)?$)(?:[6-9][0-9]|[1-9][0-9]{2,})(?:\.[0-9]{1,2})?$
Or, if you cannot afford lookaheads:
^(?:60(?:\.(?:0?[1-9]|[1-9][0-9]))|(?:6[1-9]|[7-9][0-9]|[1-9][0-9]{2,})(?:\.[0-9]{1,2})?)$
See the regex demo #1 / regex demo #2.
Details:
^ - start of string
(?!60(?:\.00?)?$) - a negative lookahead that fails the match if the string is 60, 60.0 or 60.00
(?: - start of a non-capturing group:
[6-9][0-9] - 60 to 99 number
| - or
[1-9][0-9]{2,} - a digit from 1 to 9 and then two or more digits (100 and more)
) - end of the group
(?:\.[0-9]{1,2})? - an optional sequence of a . and one or two digits
$ - end of string.
I have a regex
\(?\+\(?49?\)?[ ()]?([- ()]?\d[- ()]?){11}
This correctly matches German phone code like
+491739341284
+49 1739341284
(+49) 1739341284
+49 17 39 34 12 84
+49 (1739) 34 12 84
+(49) (1739) 34 12 84
+49 (1739) 34-12-84
but fails to match 0049 (1739) 34-12-84.
I need to adjust the regular expression so that it can match numbers with 0049 as well. can anyone help me with the regex?
Try this one:
\(?\+|0{0,2}\(?49\)?[ ()]*[ \d]+[ ()]*[ -]*\d{2}[ -]*\d{2}[ -]*\d{2}
https://regex101.com/r/CHjNBV/1
However, it's better to make it accept only +49 or 0049, and throw the error message in case the number fails validation. Because if someday you will require to extend the format - it will require making the regex much more complicated.
If you want to match the variations in the question, you might use a pattern like:
^(?:\+?(?:00)?(?:49|\(49\))|\(\+49\))(?: *\(\d{4}\)|(?: ?\d){4})? *\d\d(?:[ -]?\d\d){2}$
Explanation
^ Start of string
(?: Non capture group
\+? Match an optional +
(?:00)? Optionally match 2 zeroes
(?:49|\(49\)) Match 49 or (49)
| Or
\(\+49\) Match (+49)
) Close non capture gruop
(?: Non capture group
* Match optional spaces
\(\d{4}\) Match ( 4 digits and )
| Or
(?: ?\d){4} Repeat 4 times matching an optional space and a digit
)? Close non capture group and make it optional
* Match optional spaces
\d\d Match 2 digits
(?:[ -]?\d\d){2} Repeat 2 times matching either a space or - followed by 2 digits
$ End of string
Regex demo
Or a bit broader variation matching the 49 prefix variants, followed by matching 10 digits allowing optional repetitions of what is in the character class [ ()-]* in between the digits.
^(?:\+?(?:00)?(?:49|\(49\))|\(\+49\))(?:[ ()-]*\d){10}$
Regex demo
I want to create a Regex Transport rule for the email with Subject 1365 1049126 9003175245 19382_ST03
I want to check if the subject contains 900 in the third group of characters and the numbers after 900 are dynamic and is a single word.
For example:
Subject 1 - 1365 1049126 9003175245 19382_ST03
Subject 2 - 2455 3245626 9003175000 19382_ST03
Subject 3 - 4567 4449126 9003005030 19382_ST03
How do I set this in regular expression? Any help will be appreciated.
You could use something like this: ^[0-9]{4}\s[0-9]{7}\s900[0-9]{7}\s\w{10}$
^ Matches start of strings
[0-9]{4}\s Matches 4 digits and whitespace after them
[0-9]{7}\s Does the same but for 7 digits
900[0-9]{7}\s <-- This matches 900 and 7 digits and whitespace
\w{10}s Matches 10 word characters
$ Matches end of string
If these groups are not ONLY digits, you can replace [0-9] with \w
I'm trying to figure out a regex expression that does the following. Both conditions below must be true:
1) Between 0 and 100 inclusive
2) Can contain one or two decimals only but not obligatory.
It should not allow 100.01 or 100.1
100 is the maximum value, or 100.0 or 100.00
I tried ^(100(?:\.00)?|0(?:\.\d\d)?|\d?\d(?:\.\d\d)?)$
which helped me in this question
but this does not accept 99.0 (one decimal).
I'm probably very close.
You just need to make each second decimal digit optional:
^(?:100(?:\.00?)?|\d?\d(?:\.\d\d?)?)$
^ ^
See the updated regex demo. The 0(?:\.\d\d)? alternative is covered by \d?\d(?:\.\d\d)? one (as per Sebastian's comment) and can thus be removed.
The ? quantifier matches one or zero occurrences of the subpattern it quantifies.
Pattern details:
^ - start of string
(?: - start of an alternation group:
100(?:\.00?)? - 100, 100.0 or 100.00 (the .00 is optional and the last 0 is optional, too)
\d?\d(?:\.\d\d?)? - an optional digit followed by an obligatory digit followed with an optional sequence of a dot, a digit and an optional digit.
) - end of the alternation group
$ - end of string.
BONUS: If the number can have either . (dot) or , (comma) as a decimal separator, you can replace all \. patterns in the regex with [.,]:
^(?:100(?:[.,]00?)?|\d?\d(?:[.,]\d\d?)?)$
I am using preg_match (PHP version 5.5.*) and want to ignore all alphabetic letters [a-zA-Z] and special symbols such as $ and -, only to match numbers, commas, dots. Whitespaces between numbers such as 6 000 should be matched. Commas after a number that is not followed by another number should be ignored, such as 6, would only match 6
Note that this is used in a single string and never in a list, like the sample below. I use the list to show what input and desired output is, "per line".
Sample input:
1
1,99
1.99
10
100
5999 dollars
2 USD
$2,99
Our price 2.99
Price: $ 20
200 $
20,-
6 999 USD
Desired output:
1
1,99
1.99
10
100
5999
2
2,99
2.99
20
200
20
6 999
I have tried /([0-9.,\s]+)/ but the output of 6 999 USD becomes 6.
Edit
The code we are using looks like this:
preg_match($regex, $value, $extractions);
array_shift($extractions);
$this->persist($extractions);
Demo
Update:
If you have instead of spaces, you can do two things..my recommended is to just do a str_replace() first:
str_replace(' ', ' ', $number);
The other option is to also check for with the [\s,] group:
[\d.](?:[\d.]|(?:[\s,]| )(?=\d))*
Example:
preg_match('/[\d.](?:[\d.]|[\s,](?=\d))*/', $number, $matches);
$number = reset($matches);
Explanation:
So I classified the valid characters (digits, spaces, commas, and periods) into two groups: [\d.] and [\s,]. A number must start with a digit or a period ($.99 == .99 != 99). Then we use a repeated non-capturing group (?:...)* to take care of our alternation and lookahead assertions. Anytime there is a [\d.] we match it with now questions asked. Otherwise (|), it it is a [\s,] we assert that it is followed with a digit using a lookahead ((?=...)).
Demo
Example:
preg_replace('/\s*[^\d\s,.]+\s*|,(?!\d)/', '', $number);
Explanation:
[^\d\s,.]+ will match 1+ characters that are not either a digit, whitespace, a comma, or a period. We put \s* on either side to grab any extra whitespace around these unwanted characters (like in "Our price "). The only unwanted character this doesn't match is a trailing comma. We use an alternation (|), then look for a comma, and then make sure that it is not followed by a digit using a negative lookahead ((?!...)).
Demo