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.
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 module
s you want to match have id
s 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.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加