0
votes

I am trying to create a PHP REGEX that will match if two words appear next to each other with ANY number of spaces between them.

For example, match "Daniel   Baylis" (3 spaces between 'Daniel' and 'Baylis'). I tried with this but it doesn't seem to work:

"/DANIEL[ ]{1,5}BAYLIS/" (this was to check up to 5 spaces which the most I expect in the data)

and

"/DANIEL[ ]{*}BAYLIS/"

I need to extract names from within larger bodies of text and names can appear anywhere within that text. User input error is what creates the multiple spaces.

Thanks all! - Dan

1
You could also just replace the obsolete whitespaces first and afterward match for it, which should be easier ;)Kenny
DANIEL[ ]{1,5}BAYLIS this looks good, why didn't work?Kent
@Kent I'm guessing case is the issue... ;)Romain
I actually used /DANIEL[ ]{1,5}BAYLIS/i in my script though I didn't post in the example, but it wasn't getting the match. @Kent - do you think this should worked too?Dan
i tested with grep -i, it worked. what i tried was ` echo "Daniel BAYLIS"|grep -iP "Daniel[ ]{1,5}Baylis"`Kent

1 Answers

3
votes

/DANIEL[ ]+BAYLIS/ should do... + will glob one or more occurence of the previous character(-class), in this case, litteral space.

Also, assuming you want to match regardless of the case, you'll need to adjust your regex to be case-insensitive, which I'm not sure how to do in PHP (it depends on which flavor of regex you use... Long time since I last touched that...)