code_junkie

Regex to capture an optional group in the middle of a block of input

Nov 13th, 2011
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. Some text %interestingbit% lots of random text lots and lots more %anotherinterestingbit%
  2. Some text %interestingbit% lots of random text OPTIONAL_THING lots and lots more %anotherinterestingbit%
  3. Some text %interestingbit% lots of random text lots and lots more %anotherinterestingbit%
  4.  
  5. %interestingbit%.+?((?<OptionalCapture>OPTIONAL_THING)).+?%anotherinterestingbit%
  6.  
  7. %interestingbit%.+?((?<OptionalCapture>OPTIONAL_THING))?.+?%anotherinterestingbit%
  8.  
  9. %interestingbit%.+?(?:(?<optionalCapture>OPTIONAL_THING).+?)?%anotherinterestingbit%
  10.  
  11. %interestingbit%.+?(?<OptionalCapture>OPTIONAL_THING)?.+?%anotherinterestingbit%
  12.  
  13. %interestingbit%.+?(?<OptionalCapture>OPTIONAL_THING|).+?%anotherinterestingbit%
  14.  
  15. %interestingbit%(?:(.+)(?<optionalCapture>OPTIONAL_THING))?(.+?)%anotherinterestingbit%
Add Comment
Please, Sign In to add comment