tags:

views:

49

answers:

3

I need to match the following pattern:

return character 5 character string, A-Z, 0-( space

For example:

ABC65 
CG876

Each with a return character before and a space after. What regular expression should I use for this? Using PHP

+1  A: 

Assuming you meant 0-9 instead of 0-(:

^[A-Z0-9]{5} $
Thomas
The $ may not be necessary.
Larry Wang
Does this match the preceding return character?
It matches "start of line" (the ^), so after a fashion "yes".
Vatine
This regex will match a `ABC65 ` at the start of the file, even though there is no preceding `\n`.
Larry Wang
A: 
$yourstring = "ABC65 CG876";
echo preg_replace('/([A-Z0-9]{5}) /', '${1}<br />', $yourstring);

It seems I misunderstood (or misinterpreted) your question. So the correct answer in my opinion is:

^[A-Z0-9 ]{5}$
qbi
+1  A: 

If you want the space everywhere :

/^[A-Z0-9 ]{5}/
M42