3

How do I optionally match the start ^ or end $ of a line in a regular expression?

For example:

/(?<=[\s\^])/ does not match starts with space character or start of line.

My problem was in PHP matching the following.

$str = '**bold** **bold** **bold**';
echo preg_replace('/(?<=\s|^)\*\*(.+?)\*\*(?=\s|$)/', '<strong>\\1</strong>', $str);

My edge cases that were the bold at the start and end of the string were not being matched. Some edge cases I came across with other variants were matching within strings, matching chains of asterisks, and countless other problems.

echo preg_replace('/(?<=^|\s|\>)[\*]{2,}(?=[^\s\*])(.+?)(?<=[^\s\*])[\*]{2,}(?=\s|\<|$)/', '<strong>\\1</strong>', $str);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
esryl
  • 159
  • 1
  • 5

2 Answers2

10

If you insist on matching after a whitespace character or the start of the line, use an alternation (not the character class). Assuming your regex flavor supports alternations in lookbehind assertions.

/(?<=\s|^)\w+/m

But probably you are looking for a word boundary, \b. They match on the start or end of a word (exactly on the change from a word character to a non word character or the other way round).

stema
  • 90,351
  • 20
  • 107
  • 135
0

Just remove the anchors. Without the anchors, the regex will be matched anywhere in the string you are searching.

Femaref
  • 60,705
  • 7
  • 138
  • 176
  • Added an example with look-behind to show what I am attempting to match, space character or start of line. – esryl Mar 31 '13 at 20:47