1

Hello All,

I am trying to widen the scope on a preg_match I currently use in a shoutbox. I am struggling to build on my current regular expression to obtain the desired identification. Please see the current regex I am using followed by some information on the matches I would like to achieve.


Current regular expression:

~\bf+(?:\.+|\s+)?r+(?:\.+|\s+)?e+(?:\.+|\s+)?d+(?:\.+|)?\b~i

Desired match info:

[01] Lorem ipsum dolor fred sit amet.

  • Identify key word.

[02] Lorem ipsum dolor $fred sit amet.

  • Identify single dollar symbol and key word.

[03] Lorem ipsum dolor $ofred sit amet.

  • Identify single dollar symbol followed by a single alphanumeric character and key word.

[04] Lorem ipsum dolor $ooofred sit amet.

  • Identify single dollar symbol followed by multiple alphanumeric characters and key word.

[05] Lorem ipsum dolor $$$ooofred sit amet.

  • Identify multiple dollar symbols followed by multiple alphanumeric characters and key word.

[06] Lorem ipsum dolor $$$ofred sit amet.

  • Identify multiple dollar symbols followed by a single alphanumeric character and key word.

[07] Lorem ipsum dolor $o$oo$$$ofred sit amet.

  • Identify any combination of dollar symbols and alphanumeric characters followed by key word.

[08] Lorem ipsum dolor $o$oo $$$ofred sit amet.

  • Spaces break the identification

[09] $ofred sit amet.

  • Identified with no leading spaces

[10] Lorem ipsum dolor $ofred

  • Identified with no trailing spaces

[11] Lorem ipsum dolor $ofred!

  • Identified with trailing symbols

Thank you for any help, it's much appreciated.

Matthew
  • 129
  • 1
  • 1
  • 8

2 Answers2

1
/((\$[\w\$]*)?fred)/

What's wrong with that regex?

I'm not sure I understand the significance of:

(?:\.+|\s+)

In your regex.

Jonathan Rich
  • 1,740
  • 10
  • 11
1

This has to be the longest explanation I have ever seen for a regex that is so small:

if (preg_match('/(?<=^|\s)(?:\bfred\b|\$[$\w]*fred\b)/x', $subject, $regs)) {
    $result = $regs[0];
}

Explanation:

"
(?<=           # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind)
               # Match either the regular expression below (attempting the next alternative only if this one fails)
      ^        # Assert position at the beginning of the string
   |           # Or match regular expression number 2 below (the entire group fails if this one fails to match)
      \s       # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
)
(?:            # Match the regular expression below
               # Match either the regular expression below (attempting the next alternative only if this one fails)
      \b       # Assert position at a word boundary
      fred     # Match the characters “fred” literally
      \b       # Assert position at a word boundary
   |           # Or match regular expression number 2 below (the entire group fails if this one fails to match)
      \$       # Match the character “$” literally
      [$\w]    # Match a single character present in the list below
               # The character “$”
               # A word character (letters, digits, etc.)
         *     # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
      fred     # Match the characters “fred” literally
      \b       # Assert position at a word boundary
)
"
FailedDev
  • 26,680
  • 9
  • 53
  • 73
  • [Red Face] I guess I'm not the best at explaining things :) Thank you very much though, your answer seems to do the job brilliantly. Much appreciated! – Matthew Dec 06 '11 at 22:45
  • @Matthew Stand by, for editing. I am improving it. There may be some cases I omitted :) – FailedDev Dec 06 '11 at 22:50
  • @Matthew The now edited regex matches **exactly** what you asked for. Nothing less/nothing more. You are welcome btw :) – FailedDev Dec 06 '11 at 22:57
  • Thanks for the revision, is there any chance of allowing space(s) in the key word, i.e. fred in this case? – Matthew Dec 06 '11 at 23:07
  • That would be a very different more complex regex. Everything is possible though. But you should probably open another question for that. – FailedDev Dec 06 '11 at 23:08