0

I'm looking for a regex expression to use with php :

I have 4 strings like this in array :

$strA = 'John P. Beck';   // name and surname
$strB = '12012 BlaBla Xx';  // street
$strC = 'Houston TX 12345-6789';  // city + zip code
$strD = '(123) 456-7890';  // telephone number

By now i'm using a regex in a for loop but it always returns me both the first and the second string, and sometimes the first and the third string....

for($i = 0; $i < count($array); $i++)
{
    if(preg_match("/[A-Z]/", $array[$i]))
    {
      echo $array[$i] . '<br>';
    }
    else echo 'no match<br>';
}

I need a regex with this conditions : - at least 1 Uppercase in string AND absolutely NO digits... to return only $strA.....

Thank you very much

EDIT

sorry maybe I explained myself bad and without adding details... This is the structure of the array (example):

[0] => <p>
[1] =>                41
[2] => <br />
[3] =>                John P. Beck
[4] => <br />
[5] =>                12012 BlaBla Xx
[6] => <br />
[7] =>                Houston TX 12345-6789
[8] => <br />
[9] =>                (123) 456-7890
[10] => <br />
[11] =>                42
[12] => <br />
[13] =>                Linda F. Green
[14] => <br />
[15] =>                1888 BlaBla Xx
[16] => <br />
[17] =>                Saint Louis, MO 12345-6789
[18] => <br />
[19] =>                (123) 456-7890
[20] => <br />
...
...
...

I need to store only the 4th string and the 14th string.. and so on ...

@nickb : with $array[0] it returns always a p tag.. i'm in a for loop...... :)

5
  • What is "joHn p. beck" ? (only the H is in upercase). Should pass or not ? Commented Jun 1, 2012 at 23:22
  • echo $array[0];. Problem solved. Commented Jun 1, 2012 at 23:23
  • It would help to know some sample inputs and desired outputs. Your edit has <br> which makes me think it is output, but what was the input? Commented Jun 2, 2012 at 0:27
  • I am not sure why you need any sort of regex at all if your array is formed like that. Why not taking every item numbered N*10+3 or every item at +2 after a string that represents a valid number? Commented Jun 2, 2012 at 11:58
  • you mean using a division and the mod operator (%) .... ? Yes I think it would be more mathematically correct but i needed a fast solution... I solved it with a regex by samsamX Commented Jun 2, 2012 at 12:24

2 Answers 2

1

0 or more chars (except digits), followed by one or more uppercase letter, followed by 0 or more chars (except digits) :

/[^0-9]*[A-Z]+[^0-9]*/

If you want your data beginning by the uppercase(s) letter(s) :

/^[A-Z]+[^0-9]*/
Sign up to request clarification or add additional context in comments.

9 Comments

Show us 2 example isn't enought, you should specify exactly what you want. Are you searching for every 3 parts names ? Like xxxxx X. xxxx or Xxxxx X. xxx or even Xxxx X. Xx ?
the name can be Linda F. Green or even Linda Green or anything else but without digits..
don't matter if there is a space in the string .. my actually regex which is "/[A-Z]/" does return the name BUT ALSO the address "12012 BlaBla Xx" that i want to discard...
/^[A-Z]+[a-zA-Z]+( [A-Z]\.)? [A-Z]+[a-zA-Z]+$/ will accept Linda F. Green and Linda Green, but NOT linda green, Linda F Green, Linda f. green
Me neither ;) You're welcome, and please set this question as answered if you're done with.
|
0

preg_match will match if any part of the string matches the regular expression. If you want to test the whole string, use ^ and $ which represent the start and end of the string.

2 Comments

The "m" regex modifier to the regex will treat each \n as a new string.
@jordanm, no, /m only changes what ^ and $ match.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.