I have a regex that looks like this:
/(((\+|00)32[ ]?(?:\(0\)[ ]?)?)|0){1}(4(60|[789]\d)\/?(\s?\d{2}\.?){2}(\s?\d{2})|(\d\/?\s?\d{3}|\d{2}\/?\s?\d{2})(\.?\s?\d{2}){2})/g
this matches: +32 16/894477 but +32 16-894477 doesn't
this 20150211-0001731015-1 also matches but this shouldn't match
I am trying to fix my regex here:
https://regex101.com/r/LmaIPA/1
(((\+|00)32[ ]?(?:\(0\)[ ]?)?)|0){1}(4(60|[789]\d)\/?(\s?\d{2}\.?){2}(\s?\d{2})|(\d\/?\s?\d{3}|\d{2}(\/?|\-)\s?\d{2})(\.?\s?\d{2}){2})
I guess I fixed part of it by adding this but let me know if there something else that doesn't work properly :)
There are a lot of capture groups, and some can also be omitted if you don't need them for after processing.
The issue is that for +32 16-894477 you are not matching the hyphen, and you match the larger string as there are no boundaries set so you get a partial match.
Some notes:
You don't have to escape the / when using a different delimiter
You can omit {1} from the pattern
\s can also match a newline, you can use \h if you want to match a horizontal whitespace char
A single space [ ] does not have to be in a character class
You can extend the pattern with adding the hyphen and forward slash to a character class using [/-]?, wrap the whole pattern in a non capture group and assert a whitspace boundary to the right (?:whole pattern here)(?!\S)
A version without the capture groups for a match only:
(?:(?:(?:\+|00)32\h?(?:\(0\)\h?)?|0)(?:4(?:60|[789]\d)/?(?:\h?\d{2}\.?){2}\h?\d{2}|(?:\d/?\h?\d{3}|\d{2}[/-]?\h?\d{2})(?:\.?\h?\d{2}){2}))(?!\S)
Regex demo | Php demo
Php example
$re = '~(?:(?:(?:\+|00)32\h?(?:\(0\)\h?)?|0)(?:4(?:60|[789]\d)/?(?:\h?\d{2}\.?){2}\h?\d{2}|(?:\d/?\h?\d{3}|\d{2}[/-]?\h?\d{2})(?:\.?\h?\d{2}){2}))(?!\S)~';
$str = 'OK 01/07 - 31/07
OK 0487207339
OK +32487207339
OK 01.07.2016
OK +32 (0)16 89 44 77
OK 016894477
OK 003216894477
OK +3216894477
OK 016/89.44.77
OK +32 16894477
OK 0032 16894477
OK +32 16/894477
NOK +32 16-894477 (this should match)
OK 0479/878810
NOK 20150211-0001731015-1 (this shouldn\'t match)';
preg_match_all($re, $str, $matches);
print_r($matches[0]);
Output
Array
(
[0] => 0487207339
[1] => +32487207339
[2] => +32 (0)16 89 44 77
[3] => 016894477
[4] => 003216894477
[5] => +3216894477
[6] => 016/89.44.77
[7] => +32 16894477
[8] => 0032 16894477
[9] => +32 16/894477
[10] => +32 16-894477
[11] => 0479/878810
)
How can I replace a string by putting a dot every two characters using the regexp_replace function?
For example:
1 => 1
12 => 12
123 => 12.3
1234 => 12.34
12345 => 12.34.5
123456 => 12.34.56
... and so on.
I tried some odds but I did not succeed.
Match (.{2})(?!$) globally and replace it with $1..
The (?!$) part is a negative look ahead preventing a match on the last two numbers. It avoids 12.34 from being 12.34..
test=> select regexp_replace('12345678', '(.{2})(?!$)', '\1.', 'g');
regexp_replace
----------------
12.34.56.78
Demo
I want to use a Regex expression to get all ranges in an IP Address provided.
Examples:
192.168.0-255.1 would return 0-255
192.168.0-255.1-10 would return 0-255 and 1-10
192.168.0-10,42,80-200.1-10,128-255 would return 0-10, 80-200, 1-10, 128-255.
BONUS: I'd also like to be able to separate these expressions into 4 different ones to determine which octet the IP range is in.
Example: 192.168-180.0.1 I'd like to get 168-180 here from an expression that looks for a match with only one period left of the substring and two periods somewhere in the right side of the substring.
Something like this?
<?php
$input = <<<INPUT
192.168.0-255.1
192.168.0-255.1-10
192.168.0-10,42,80-200.1-10,128-255
INPUT;
preg_match_all("/[0-9]+\-[0-9]+/m", $input, $m);
print_r($m);
Output:
Array
(
[0] => Array
(
[0] => 0-255
[1] => 0-255
[2] => 1-10
[3] => 0-10
[4] => 80-200
[5] => 1-10
[6] => 128-255
)
)
[0-9]+ 1 or more number
- "-" escaped
[0-9]+ 1 or more number
I am looking for a regexp option or trick to capture all possible strings in a regexp when matches can overlap.
Example : /A.A/ in string "ABACADA"
It finds : ABA, ADA and not ACA !!
I would like : ABA, ACA, ADA
I am working in PHP, but it can be applied to other languages
preg_match_all('/A.A/',"ABACADA",$matches);
var_dump($matches[0]);
// output : array (size=2)
// 0 => string 'ABA' (length=3)
// 1 => string 'ADA' (length=3)
Can you help me? Thanks
You can use a positive lookahead assertion to get all 3 matches:
(?=(A.A))
RegEx Demo
For your input it finds 3 matches in captured group #1:
ABA
ACA
ADA
PHP Code:
if (preg_match_all('/(?=(A.A))/', "ABACADA", $m))
print_r($m[1]); // printing index 1
Output:
Array
(
[0] => ABA
[1] => ACA
[2] => ADA
)
I've a regex that matches comma separated numbers with an optional two digit decimal part in a given multiline text.
/(?<=\s|^)\d{1,3}(,\d{3})*(\.\d{2})?(?=\s|$)/m
It matches strings like 1, 12, 12.34, 12,345.67 etc successfully. How can I modify it to match a number with only the decimal part like .23?
EDIT: Just to clarify - I would like to modify the regex so that it matches 12, 12.34 and .34
And I am looking for 'stand alone' valid numbers. i.e., number-strings whose boundaries are either white space or start/end of line/string.
This:
\d{1,3}(,\d{3})*(\.\d\d)?|\.\d\d
matches all of the following numbers:
1
12
.99
12.34
12,345.67
999,999,999,999,999.99
If you want to exclude numbers like 123a (street addresses for example), or 123.123 (numbers with more than 2 digits after the decimal point), try:
(?<=\s|^)(\d{1,3}(,\d{3})*(\.\d\d)?|\.\d\d)(?=\s|$)
A little demo (I guessed you're using PHP):
$text = "666a 1 fd 12 dfsa .99 fds 12.34 dfs 12,345.67 er 666.666 er 999,999,999,999,999.99";
$number_regex = "/(?<=\s|^)(?:\d{1,3}(?:,\d{3})*(?:\.\d\d)?|\.\d\d)(?=\s|$)/";
if(preg_match_all($number_regex, $text, $matches)) {
print_r($matches);
}
which will output:
Array
(
[0] => Array
(
[0] => 1
[1] => 12
[2] => .99
[3] => 12.34
[4] => 12,345.67
[5] => 999,999,999,999,999.99
)
)
Note that it ignores the strings 666a and 666.666
/(?<=\s|^)(\d{1,3}(,\d{3})*(\.\d{2})?|\.(\d{2}))(?=\s|$)/m
Or taking into account some countries where . is used as a thousand seperator, and , is used as a decimal seperator
/(?<=\s|^)(\d{1,3}(,\d{3})*(\.\d{2})?|\d{1,3}(\.\d{3})*(,\d{2})?|\.(\d{2})|,(\d{2}))(?=\s|$)/m
Insane Regex for Internationalisation
/((?<=\s)|(?<=^))(((\d{1,3})((,\d{3})|(\.\d{3}))*(((?<=(,\d{3}))(\.\d{2}))|((?<=(\.\d{3}))(,\d{2}))|((?<!((,\d{3})|(\.\d{3})))([\.,]\d{2}))))|([\.,]\d{2}))(?=\s|$)/m
Matches
14.23
14,23
114,114,114.23
114.114.114,23
Doesn't match
14.
114,114,114,23
114.114.144.23
,
.
<empty line>
This answer treats with this question more comprehensively.
(#"^((([0-9]+)(.([0-9]+))?)(\,(([0-9]+)(.([0-9]+))?))*)$")
This works for comma separated whole number or comma separated decimal numbers.
Example:
Happy scenarios:
case 1) 9,10
case 2) 10.1,11,12,15,15.2
case 3) 9.8
case 4) 9
Sad scenarios:
case 1) 2..7
case 2) 2,,7
case 3) 2.
case 4) 7,
case 5) ,
case 6) .
case 7) .2
case 8) ,2