Matthew
Matthew

Reputation: 129

PHP Regular Expression Improvement 2.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

Upvotes: 0

Views: 58

Answers (1)

mario
mario

Reputation: 145482

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.

Upvotes: 3

Related Questions