I'm trying to "validate" some input from a form on the backend but I kinda got stuck with the required regex. Basically, I want to get a number, no leading zeroes, with an optional decimal and an optional case insensitive thousands notation (1,000 = 1k, 1,000,000 = 1m, 1 billion = 1b ...)
These should match/validate:
12.39
12.389k
99.1003b
40.1m
These should fail.
0.0
12.00b
1e3
2^5
0xFF
12.6z
asdf
So far I've tried this:
^[1-9]\d*(?:\.\d+)?(?:[kmbt])?$
It works but I'm thinking maybe someone could solve this a bit more elegantly than I did.
[Edit] without thousand separator:
/^(?:[1-9][0-9]*(?:\.[0-9]*[1-9])?|0\.[0-9]*[1-9])[kbmt]?$/i
demo
[Old answer] with thousand separator:
You can do it with:
/^(?:[1-9][0-9]{0,2}(?:(,?)[0-9]{3})?(?:\1[0-9]{3})*(?:\.[0-9]*[1-9])?|0\.[0-9]*[1-9])[kbmt]?$/i
demo
details:
^
(?: # numbers >= 1
[1-9][0-9]{0,2}
(?:(,?)[0-9]{3})? # capture an eventual thousand separator
(?:\1[0-9]{3})*
# decimal
(?:\.[0-9]*[1-9])?
| # numbers < 1
0\.[0-9]*[1-9]
)[kbmt]?$
Update in REGEX.
Below regex will not leading zero allow special characters and characters without specified for notation.
Below regex will allow decimal and integer with thousand and other notation.
^[1-9]\d*(\.[0-9]+)?(?:[kmbtKMBT])?$
Please check working demo : http://www.regexpal.com/?fam=95995
The below should be what you're after:
/(\b[1-9][\d\,\.]+([kmbt]?))(\s)/g
It matches 12.00b however... Here is an example of it's use.
Group 1 will contain your exact match.
Related
I an trying to get a regex for a phone number with exactly 12 digits in the format: +############.
Code i am trying to use is ([+]?)\d{12}(\d{2})?$ but no luck.
Please help
This pattern will match exactly 12 digits after a plus sign:
/^\+\d{12}$/
What is your trailing optional (/d{2})? component doing in your pattern?
This is the same functionality without regex:
$phone='+012345678912';
if($phone[0]=='+' && strlen($phone)==13 && is_numeric(substr($phone,1))){
echo 'valid';
}else{
echo 'invalid';
}
// displays: valid
Try this:
^\+\d{12}(\d{2})?$
You needed to escape the plus sign
Regex101 Demo
Do we need to capture certain digits/sequences or are you just validating its a number with that format?
I use this online tool regex101 whenever I'm unsure of regex. It shows on the right exactly what you're capturing/checking which is very useful. Depending on your use case, I don't see how this regex doesn't work, please provide an example. Otherwise:
You're only capturing the + sign and the 2 digits after the initial 12.
You anchor to the end of the string and not the beginning
Both the + and 2 extra numerals are optional but you wanted to get the +############ exact?
I suggest you use \+(\d{12}) and avoid using anchors and capture groups you do not require.
If you want to support optional spaces between sets of three numbers, you would use this regex instead
^(\+)(\d{3}\s?){4}(\d{2})?$
where \s is a space character and ? means optional
https://regex101.com/r/nX5XnH/3 (demo)
+012 345 678 912 (ok)
+012345 678 912 (ok)
+012 345678912 (ok)
+01234567891244 (ok)
012345678913 (no mach - missing plus sign)
I'm trying to remove / detect phone numbers from messages between users of my marketplace website (think eBay does something similar)
this is the code I'm using:
$string = preg_replace('/([0-9]+[\- ]?[0-9]+)/', '', $string);
BUT... it's too aggressive and it does strip away any number with 2 or more numerals... how can set a limit of say 7 numbers instead?
to be more precise the phone numbers can be any format like
3747657654
374-7657654
374-765-7654
(374)765-7654
etc...(i cannot predict what the users will write depending of their habits)
Try this regular expression :
/([0-9]+[\- ]?[0-9]{6,})/
changed to match your samples:
Regex101
That would depend on the exact requirements as now you have 1 or more numbers followed by an optional - or space followed by 1 or more numbers again.
If you wanted for example at least 2 numbers before the space or - followed by at least 5 numbers, you could use something like:
$string = preg_replace('/([0-9]{2,}[\- ]?[0-9]{5,})/', '', $string);
^^^^ Here you can specify mininimum / maximum
^^^^ Here you can specify mininimum / maximum
You can try something like this:
$string = preg_replace('/(?<![0-9]|[0-9]-)[0-9](?:[- ]?[0-9]){6}(?!-?[0-9])/', '', $string);
The lookarounds are here to avoid numbers with more than 7 digits, but if you want something more specific, you should provide an example string.
It is impossible to determine whether a number of X digits (where X is a valid phone number length) is a phone number or something else without some sort of context intelligence happening. A simple regex can't determine the difference between "call me at 3453456" and "call me when you've flown 3453456 miles".
Therefore trying to catch phone numbers without any formatting (just straight digits) with a regex is hopeless, pure and simple. Attempting to do so is only holding you back from finding a regex that can find formatted/semi-formatted numbers. What you should be going for here is "get the obvious and as many others as possible with minimal false positives...but recognize I can't get them all."
For that I'd recommend this:
/1?[ \-]?\(?([0-9]{3})?\)?[ \-]?([0-9]{3})[ \-]([0-9]{4})/g
It should not get the first three, but get all the rest in this list:
no-match: 3747657654
no-match: 444444444444444
no-match: 7657654
match: 374-765-7654
match: 1-374-765-7654
match: (374)765-7654
match: (374) 765 7654
match: 765-7654
match: 1 (374) 765 7654
match: 1(374)765 7654
Is there a function or a easy way to strip down phone numbers to a specific format?
Input can be a number (mobile, different country codes)
maybe
+4917112345678
+49171/12345678
0049171 12345678
or maybe from another country
004312345678
+44...
Im doing a
$mobile_new = preg_replace("/[^0-9]/","",$mobile);
to kill everything else than a number, because i need it in the format 49171 (without + or 00 at the beginning), but i need to handle if a 00 is inserted first or maybe someone uses +49(0)171 or or inputs a 0171 (needs to be 49171.
so the first numbers ALWAYS need to be countryside without +/00 and without any (0) between.
can someone give me an advice on how to solve this?
You can use
(?:^(?:00|\+|\+\d{2}))|\/|\s|\(\d\)
to match most of your cases and simply replace them with nothing. For example:
$mobile = "+4917112345678";
$mobile_new = preg_replace("/(?:^(?:00|\+|\+\d{2}))|\/|\s|\(\d\)/","",$mobile);
echo $mobile_new;
//output: 4917112345678
regex101 Demo
Explanation:
I'm making use of OR here, matching each of your cases one by one:
(?:^(?:00|\+|\+\d{2})) matches 00, + or + followed by two numbers at the beginning of your string
\/ matches a / anywhere in the string
\s matches a whitspace anywhere in the string (it matches the newline in the regex101 demo, but I suppose you match each number on its own)
\(\d\) matches a number enclosed in brackets anywhere in the string
The only case not covered by this regex is the input format 01712345678, as you can only take a guess what the country specific prefix can be. If you want it to be 49 by default, then simply replace each input starting with a single 0 with the 49:
$mobile = "01712345678";
$mobile_new = preg_replace("/^0/","49",$mobile);
echo $mobile_new;
//output: 491712345678
This pattern (49)\(?([0-9]{3})[\)\s\/]?([0-9]{8}) will split number in three groups:
49 - country code
3 digits - area code
8 digits - number
After match you can construct clean number just concatnating them by \1\2\3.
Demo: https://regex101.com/r/tE5iY3/1
If this not suits you then please explain more precisely what you want with test input and expected output.
I recommend taking a look at LibPhoneNumber by Google and its port for PHP.
It has support for many formats and countries and is well-maintained. Better not to figure this out yourself.
https://github.com/giggsey/libphonenumber-for-php
$phoneUtil = \libphonenumber\PhoneNumberUtil::getInstance();
$usNumberProto = $phoneUtil->parse("+1 650 253 0000", "US");
I have an array of numbers, for example:
10001234
10002345
Now I have a number, which should be matched against all of those numbers inside the array. The number could either be 10001234 (which would be easy to match), but it could also be 100001234 (4 zeros instead of 3) or 101234 (one zero instead of 3) for example. Any combination could be possible. The only fixed part is the 1234 at the end.
I cant get the last 4 chars, because it can also be 3 or 5 or 6 ..., like 1000123456.
Whats a good way to match that? Maybe its easy and I dont see the wood for the trees :D.
Thanks!
if always the first number is one you can use this
$Num=1000436346;
echo(int)ltrim($Num."","1");
output:
436346
$number % 10000
Will return the remainder of dividing a number by 10000. Meaning, the last four digits.
The question doesn't make the criteria for the match very clear. However, I'll give it a go.
First, my assumptions:
The number always starts with a 1 followed by an unknown number of 0s.
After that, we have a sequence of digits which could be anything (but presumably not starting with zero?), which you want to extract from the string?
Given the above, we can formulate an expression fairly easily:
$input='10002345';
if(preg_match('/10+(\d+)/',$input,$matches)) {
$output = $matches[1];
}
$output now contains the second part of the number -- ie 2345.
If you need to match more than just a leading 1, you can replace that in the expression with \d to match any digit. And add a plus sign after it to allow more than one digit here (although we're still relying on there being at least one zero between the first part of the number and the second).
$input='10002345';
if(preg_match('/\d+0+(\d+)/',$input,$matches)) {
$output = $matches[1];
}
working on a project right now where we have large amount of text strings that we must localize phone numbers and make them clickable for android phones.
The phone numbers can be in different formats, and have different text before and after them. Is there any easy way of detecting every kind of phone number format? Some library that can be used?
Phone numbers can show like, has to work with all these combinations. So that the outcome is like
number
+61 8 9000 7911
+2783 207 5008
+82-2-806-0001
+56 (2) 509 69 00
+44 (0)1625 500125
+1 (305)409 0703
031-704 98 00
+46 31 708 50 60
Perhaps something like this:
/(\+\d+)?\s*(\(\d+\))?([\s-]?\d+)+/
(\+\d+)? = A "+" followed by one or more digits (optional)
\s* = Any number of space characters (optional)
(\(\d+\))? = A "(" followed by one or more digits followed by ")" (optional)
([\s-]?\d+)+ = One or more set of digits, optionally preceded by a space or dash
To be honest, though, I doubt that you'll find a one-expression-to-rule-them-all. Telephone numbers can be in so many different formats that it's probably impractical to match any possible format with no false positives or negatives.
Not sure that there is a library for that. Hmmmm. Like any international amenities, telephone numbers are standardised and there should be a format defining telephone numbers as well. E.164 suggests recommended telephone numbers: http://en.wikipedia.org/wiki/E.164 . All open-source decoding libraries are built from reading these standard formats, so it should be of some help if you really cant find any existing libs
I guess this might do it for these cases?
preg_replace("/(\+?[\d-\(\)\s]{7,}?\d)/", 'number', $str);
Basicly I check if it may start on +. It doesn't have to. Then I check if it got numbers, -, (, ) and spaces with at least 8 cases so it doesn't pick low non-phone numbers.
Try the following:
preg_match_all('/\+?[0-9][\d-\()-\s+]{5,12}[1-9]/', $text, $matches);
or:
preg_match_all('/(\+?[\d-\(\)\s]{8,20}[0-9]?\d)/', $text, $matches);