0

Hello All,

Thanks to @FailedDev I currently have the regex below which is used within a preg_match for a shoutbox. What I am trying to achieve in this question is allowing the regex to be case insensitive and give it the ability to allow the use of space(s) in the 'key word', which in this case is fred.

/(?<=^|\s)(?:\bfred\b|\$[$\w]*fred\b)/x


For background info please see the reference link.

Reference


Thank you for any help on this.


Update: Thanks to some helpful information, I have come up with the following regex that does what I need, though I feel it is not the most efficient solution.

~(?:(?<=\s|^)[$\S]*|\b)f+(?:\.+|\s+)?r+(?:\.+|\s+)?e+(?:\.+|\s+)?d+(?:\.+|)?\b~i

Community
  • 1
  • 1
Matthew
  • 129
  • 1
  • 1
  • 8

1 Answers1

3

If you want to make it case insensitive, use the /i modifier.

To allow extra whitespace, use \s* for a variable number of whitespace characters, or [ ]? for a single optional space.

See also the manual on preg_match and the PCRE syntax overview and http://regular-expressions.info/ for a tutorial. Check also the reference question Is there anything like RegexBuddy in the open source world? for a list of tools to aid with crafting regular expressions. And some useful online tools.

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
  • 1
    Nah, not really. There are way more competent people around here. I just like hyperlinks. – mario Dec 06 '11 at 23:38
  • Thanks for your reply. From the info you supplied I have come up with the following though I feel it is not the most efficent solution, it is a solution that works: `~(?:(?<=\s|^)[$\S]*|\b)f+(?:\.+|\s+)?r+(?:\.+|\s+)?e+(?:\.+|\s+)?d+(?:\.+|)?\b~i` – Matthew Dec 07 '11 at 12:20
  • @Matthew: Instead of your `?:` groups, just use a charclass `[.\s]*` – mario Dec 07 '11 at 15:18