1

I have a legacy code, that uses regular expressions to search for certain text patterns within large texts and replace them with links. These expressions have numerous optional parameters, such as

(free.?)?(online.?)?Blackjack

To speed things up I want to do a quick check with plain old strpos and look for required part of the expression (in this case 'blackjack') before running the lengthy replacement with preg_replace. Is there a way to process an expression itself and extract only required part(s)? I think it's done by yet another regular expression, but just in case, I'm using PHP.

Oleg Shemetov
  • 490
  • 2
  • 15
  • Something like `(\([^)]*\)\?)` ? – Thomas Ayoub Jan 26 '16 at 15:22
  • I don't believe a regular expression for parsing regular expressions is a good idea. If it is possible (which is might be), it will not be faster than what your are currently doing. That being said, this question/answer might have some information about parsing a regular expression into parts. http://stackoverflow.com/questions/4594135/a-parser-for-regular-expressions-in-php. – Scott Jan 26 '16 at 15:29
  • @Thomas this seems to select the optional parts :) but that's certainly a starting point for me, thanks! – Oleg Shemetov Jan 26 '16 at 17:56
  • @Scott point is, this 'keyword' can be saved along with the expression itself so the parsing will only occur once. thanks for the link, definitely a few useful snippets there. – Oleg Shemetov Jan 26 '16 at 17:59

1 Answers1

0

You may replace your original optional parts from (\([^)]*\)\?) to blank, which will leave you with the required parts.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142