I am trying to find a number that consists of only 8 numbers, this is the code I have already:
preg_match_all("/([0-9]{8})/", $string, $match)
but this pulls 8 numbers from number strings that are longer than 8 digits
any help would be gratefully appreciated
Thanks
I'll use \d rather than [0-9].
If your string should contain nothing but a number of eight digits
Use ^ and $ to match start and end of string, respectively:
preg_match_all('/^(\d{8})$/', $string, $match)
If, within a larger string, you're matching a number that should have a maximum of eight digits
Quick but slightly brutish approach:
Use \D ([^0-9]) to match "not-a-number":
preg_match_all('/^|\D(\d{8})\D|$/', $string, $match)
Lookbehinds/lookaheads might make this better:
preg_match_all('/(?<!\d)(\d{8})(?!\d)/', $string, $match)
You need word boundaries
/\b[0-9]{8}\b/
Example:
$string = '34523452345 2352345234 13452345 45357567567567 24573257 35672456';
preg_match_all("/\b[0-9]{8}\b/", $string, $match);
print_r($match);
Output:
Array
(
[0] => Array
(
[0] => 13452345
[1] => 24573257
[2] => 35672456
)
)
This might be better than the other two suggestions:
preg_match_all('/(?<!\d)(\d{8})(?!\d)/', $string, $match)
Note that \d is equivalent to [0-9].
preg_match_all("/(?:^|\D)(\d{8})(?:\D|$)/", $string, $match);
Where the start and end non-matching groups (?:) allow for any non-digit (\D) or the start (^) or end ($) of the string.
Maybe include anything but digits before and after.
preg_match_all("/[^\d]([\d]{8})[^\d]/", $string, $match)
Related
I'm not very good at regex but i have a string like this :
$str = '<span id="MainStatuSSpan" style="background: brown;"> Incoming: 012345678 Group- SUPERMONEY Fronter: - 992236 UID: Y3281602190002004448</span>';
$pattern = '/(?:Fronter: - )[0-9]{1,6}/i';
preg_match($pattern, $str, $matches);
print_r($matches);
/*** ^^^^^^^ This prints :*/
Array ( [0] => Fronter: - 992236 )
In case of the Fronter is not with - or spaces I don't get the Fronter - number.
Can anyone help with an example that works in any case, there is always a Fronter and a number.
you can use Fronter:\W*[0-9]{1,6}
Fronter:\W*[0-9]{1,6} : match Fronter:
\W* : zero or more non-word characters
[0-9]{1,6} one to six digits
you regex will also find a match with Fronter:99222236 so you must use \b to avoid overflow digit length
Fronter:[- ]*[0-9]{1,6}\b
I am trying a simple regex to match exactly 5 digits from a string. However, this pattern matches for 5 and more than 5.
preg_match_all('#[0-9]{5}+#', 'one two 412312 three (51212 four five)', $matches);
print_r($matches);
Result:
Array(
[0] => Array
(
[0] => 41231
[1] => 51215
)
)
I need it to match exactly 5 digits.
Thanks.
You can use word boundaries here and remove the + quantifier after the range operator.
preg_match_all('~\b\d{5}\b~', $str, $matches);
As stated in the comments, if you need to match the five digits in a51212a but not 412312 you can use a combination of lookaround assertions.
preg_match_all('~(?<!\d)\d{5}(?!\d)~', $str, $matches);
Try this:
preg_match_all('(\b\d{5}\b)', 'one two 412312 three (51212 four five)', $matches);
print_r($matches);
It matches every group of 5 digits.
You can use lookaheads and -behinds for that: (?<!\d)[0-9]{5}(?!\d)
I'm trying to get the string that match with original and with number in the end.
I got these strings:
mod_courts2
mod_courts_config
mod_courts_config2
From these strings I want the one that matches only with "mod_courts" with number in the end.
I'm doing this:
if (strpos($t, "mod_courts") !== FALSE) {
preg_match('/^\w+(\d+)$/U', $t, $match);
echo $match;
}
This returns me "mod_courts2" and "mod_courts_config2", I just want "mod_courts2"
Use the following regex:
/^[a-z]+_[a-z]+(\d+)$/
Explanation:
^ - assert position at the beginning of the string
[a-z]+ - match any alphabet one or more times
_ - match a literal undescore character
[a-z]+ - match any alphabet one or more times
(\d+) - match (and capture) any digit from 0 to 9 one or more times
$ - assert position at the end of the string
Test cases:
$array = array(
'mod_courts2',
'mod_courts_config',
'mod_courts_config2'
);
foreach ($array as $string) {
if(preg_match('/^[a-z]+_[a-z]+(\d+)$/i', $string, $matches)) {
print_r($matches);
}
}
Output:
Array
(
[0] => mod_courts2
[1] => 2
)
Very simply, you can do:
/^(mod_courts\d+)$/
However, if you want exactly the following format: sometext_somettext2, you can use the following regex:
/^([a-zA-Z]+_[a-zA-Z]+\d+)$/
or
/^([^_]+_[^_]+\d+)$/
Demos
http://regex101.com/r/jP8iC1
http://regex101.com/r/tI1uX8
http://regex101.com/r/fX8pO5
^mod_courts\d+$
this should do it
You can just use
^mod_courts[0-9]+$
Meaning mod_courts followed by a number (and only that, thanks to ^$ matching the beginning and end of the string). No need for the strpos check.
I'm attempting to pull a certain part out of different varying strings, and am having a really hard time getting the correct regex to do so. Here are a few examples of what I am trying to pull from:
AG055.MA - MAGNUM (Want to return just MA)
WI460.16 - SOMETHING (Want to return 16)
AG055.QB (Want to return QB)
So basically, I just want to pull the characters after the period, but before the space. Nothing else before or after. Can someone give me a hand with getting the correct regex?
This should work:
<?php
preg_match( '/\.([^ ]+)/', $text, $matches );
print_r( $matches );
?>
Output:
Array
(
[0] => .MA
[1] => MA
)
Array
(
[0] => .16
[1] => 16
)
Array
(
[0] => .QB
[1] => QB
)
The regex is saying find a . character, then get any characters after it that are not a space character. The + makes it only return matches where there is a non-space character after the dot.
preg_match('/\w+\.(\w{2})\s/', $input, $matches);
echo $matches[1];
\w+ means 1 or more word characters (a-z, A-Z and 0-9).
\. means the period/dot (the backslash is to escape it, because \. is used as an operator in regex)
(\w{2}) matches 2 word characters
\s means whitespace
preg_match('/^[A-Z0-9]{5}\.([A-Z0-9]{2})/', $string, $matches);
var_dump($matches);
Should return the characters in $matches[1].
I have some string data with alphanumeric value. like us01name, phc01name and other i.e alphabates + number + alphabates.
i would like to get first alphabates + number in first string and remaining on second.
How can i do it in php?
You can use a regular expression:
// if statement checks there's at least one match
if(preg_match('/([A-z]+[0-9]+)([A-z]+)/', $string, $matches) > 0){
$firstbit = $matches[1];
$nextbit = $matches[2];
}
Just to break the regular expression down into parts so you know what each bit does:
( Begin group 1
[A-z]+ As many alphabet characters as there are (case agnostic)
[0-9]+ As many numbers as there are
) End group 1
( Begin group 2
[A-z]+ As many alphabet characters as there are (case agnostic)
) End group 2
Try this code:
preg_match('~([^\d]+\d+)(.*)~', "us01name", $m);
var_dump($m[1]); // 1st string + number
var_dump($m[2]); // 2nd string
OUTPUT
string(4) "us01"
string(4) "name"
Even this more restrictive regex will also work for you:
preg_match('~([A-Z]+\d+)([A-Z]+)~i', "us01name", $m);
You could use preg_split on the digits with the pattern capture flag. It returns all pieces, so you'd have to put them back together. However, in my opinion is more intuitive and flexible than a complete pattern regex. Plus, preg_split() is underused :)
Code:
$str = 'user01jason';
$pieces = preg_split('/(\d+)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($pieces);
Output:
Array
(
[0] => user
[1] => 01
[2] => jason
)