Is there any way to make regex not return here? As in, I want it to not return strings that are not exactly 8 digits.
preg_match_all( '/\w+\d{8}', 'word123456789', ret );
\w+ will also match digits. If you want to return only strings that END in exactly 8 digits, then perhaps:
'/\b[A-Za-z]+\d{8}\b/'
Edit: That should read strings that start with only letters and end with exactly eight digits. If you want something else, please clarify
You could use something along these lines:
'\w+(?<!\d)\d{8}\b'
\w+ - Any word character occuring 1 or more times
(?<!\d)\d{8} - Any 8 digits not preceded by another digit.
\b - word boundary.
word12345678 - Match
word123456789 - No Match
1word12345678 - Match
1w123456789rd12345678 - Match
Related
I want to extract an 8 digit number from a string using regex.
Test strings are:
hi 82799162
236232 (82342450)
test data 8979
Required respective output should be
82799162
82342450
null
I have tried following code:
preg_match('/[0-9]{8}$/', $string, $match);
preg_match('/\d{8}$/', $string, $match);
But neither retrieves the number from 236232 (82342450).
If a regex is to capture exactly 8 digits, is must contain:
\d{8} as a central part,
a "before" condition, ensuring that no digit occurs before your match,
an "after" condition, ensuring that no digit occurs after your match.
One of possible solutions is to use negative lookbehind / lookahead:
(?<!\d)\d{8}(?!\d)
Another option is word boundary assertions (at both ends):
\b\d{8}\b
I think, regex like [0-9]{8} is not enough, as it captures also
first 8 digits from a longer sequence of digits.
Are you happy with that?
The problem is with your $ sign, and it is used to indicate the end of your expression. So basically, with that expression, you are looking for a string which ends with a 8 digit number. But in your second test string; '236232 (82342450)', ends with a bracket, and therefore it doesn't match the criteria (does not end with a number).
So remove the trailing $ and it will work.
preg_match('/[0-9]{8}/',$string,$match);
Hope it helps!!
I only want to get the - and space after - before the 4 numbers. I made the following regex to try and match these characters. ^(- )+?(?=\d{4})$
if i try this regex on the number string below i get no matches.
- 7575
what am i doing wrong?
I quite am new to regex.
Thanks in advance.
What your actual regex does is :
^(- )+? => match a sequence of -
Which has to be followed by 4 digit (?=\d{4}) without matching it
Then ending sentence $
So it's impossible.
You either , if you dont want to match the digit, have to put the end in the positive lookahead like
^(- )+?(?=\d{4}$)
Or remove the positive lookahead like
^(- )+?\d{4}$
I wanted to use regular expression to check if a string has a word that contains 8 digit of alphanumeric character, ignoring uppercase and lowercase (meaning that 2HJS1289 and 2hjs1289 should match). I know I can use preg to do this, and so far I have this:
preg_match('/[A-Za-z0-9]/i', $string)
I am unsure however on how to limit it only to 8 digits/character scheme.
For exactly 8 char word you will need to use word boundaries: \b
preg_match('/\b[A-Z\d]{8}\b/i', $string)
Try
preg_match('/\b([A-Z0-9]{8})\b/i', $string)
The {8} matches exactly 8 times. I added the capturing group (the parentheses), in case you needed to extract the actual match.
You can also use {min,max} to match the pattern repeating between min and max times (inclusive, I think). Or you can leave one of the parameters out to leave it open ended. Eg {min,} to match at least min times
[a-zA-Z0-9] - will match upper or lowercase letters or numbers
{8} - will specify to match 8 of the preceeding token
put it together:
preg_match('/([A-Za-z0-9]{8})/i', $string)
example
I'm trying to work out a regex pattern to search a string for a 12 digit number. The number could have any number of other characters (but not numbers) in front or behind the one I am looking for.
So far I have /([0-9]{12})/ which finds 12 digit numbers correctly, however it also will match on a 13 digit number in the string.
the pattern should match 123456789012 on the following strings
"rgergiu123456789012ergewrg"
"123456789012"
"#123456789012"
"ergerg ergerwg erwgewrg \n rgergewrgrewg regewrge 123456789012 ergwerg"
it should match nothing on these strings:
"123456789012000"
"egjkrgkergr 123123456789012"
What you want are look-arounds. Something like:
/(?<![0-9])[0-9]{12}(?![0-9])/
A lookahead or lookbehind matches if the pattern is preceded or followed by another pattern, without consuming that pattern. So this pattern will match 12 digits only if they are not preceded or followed by more digits, without consuming the characters before and after the numbers.
/\D(\d{12})\D/ (in which case, the number will be capture index 1)
Edit: Whoops, that one doesn't work, if the number is the entire string. Use the one below instead
Or, with negative look-behind and look-ahead: /(?<!\d)\d{12}(?!\d)/ (where the number will be capture index 0)
if( preg_match("/(?<!\d)\d{12}(?!\d)/", $string, $matches) ) {
$number = $matches[0];
# ....
}
where $string is the text you're testing
Can someone explain me the meaning of this pattern.
preg_match(/'^(d{1,2}([a-z]+))(?:s*)S (?=200[0-9])/','21st March 2006','$matches);
So correct me if I'm wrong:
^ = beginning of the line
d{1,2} = digit with minimum 1 and maximum 2 digits
([a-z]+) = one or more letters from a-z
(?:s*)S = no idea...
(?= = no idea...
200[0-9] = a number, starting with 200 and ending with a number (0-9)
Can someone complete this list?
Here's a nice diagram courtesy of strfriend:
But I think you probably meant ^(\d{1,2}([a-z]+))(?:\s*)\S (?=200[0-9]) with the backslashes, which gives this diagram:
That is, this regexp matches the beginning of the string, followed by one or two digits, one or more lowercase letters, zero or more whitespace characters, one non-whitespace character and a space. Also, all this has to be followed by a number between 2000 and 2009, although that number is not actually matched by the regexp — it's only a look-ahead assertion. Also, the leading digits and letters are captures into $matches[1], and just the letters into $matches[2].
For more information on PHP's PCRE regexp syntax, see http://php.net/manual/en/pcre.pattern.php
regular-exressions.info is very helpful resource.
/'^(d{1,2}([a-z]+))(?:s*)S (?=200[0-9])/
(?:regex) are non-capturing parentheses; They aren't very useful in your example, but could be used to expres things like (?:bar)+, to mean 1 or more bars
(?=regex) does a positive lookahead, but matches the position not the contents. So (?=200[0-9]) in your example makes the regex match only dates in the previous decade, without matching the year itself.