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.

1
  • Are you sure you don't want tu use XML with an XML parser ? Commented Nov 20, 2018 at 7:21

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.