2

Let's say I have this string:

[section][module_text id="123"]text[/module][module_number id=""]000[/module][/section]

I can find each entire [module...[/module...] substring with

(\[module)(.*?)(\[\/module.*?\])

But I would like to only match the [module] string that has a digit in the id="" parameter. In this case the first [module].

I've been trying a bunch of stuff but i cant seem to get any closer.

Markus Finell
  • 328
  • 1
  • 3
  • 14

1 Answers1

3

After matching module, you can match non-] characters until you get to the id part, and after the id's ", you can match [^"]*\d to ensure that the id has a digit in it:

(\[module[^]]*id="[^"]*\d)(.*?)(\[\/module[^]]*\])

https://regex101.com/r/25JcEP/1

If the modules you want to match have ids which always start with digits, then you can simplify it a bit to

(\[module[^]]*id="\d)(.*?)(\[\/module[^]]*\])

Note the use of a negative character class rather than .*? for the final (\[\/module[^]]*\]) - negative character classes are a bit more efficient than lazy repetition when possible.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320