Regex for strings containing substring that contains substring

Markus Finell

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.

CertainPerformance

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.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Ramda, check(filter) if array of strings contains substring

分類Dev

Get substring using regex

分類Dev

Regex multiple match substring

分類Dev

regex match substring unless another substring matches

分類Dev

Check if String contains substring in clojure

分類Dev

JScript regex - extract a substring between 2 words containing 1 or more occurrences of another word

分類Dev

Regex: Getting Variable from Substring

分類Dev

Javascript Extract from substring regex

分類Dev

Replace strings in a string by a substring of those strings

分類Dev

Filter an array of objects by a property containing a substring in AngularJS

分類Dev

How to check whether a string contains a substring in Kotlin?

分類Dev

Pandas: If a row contains this substring, update a cell in this row

分類Dev

Look for file in directory that contains a certain substring

分類Dev

use lodash to find substring from array of strings

分類Dev

Check list of strings for substring, case-insensitive

分類Dev

Sort dataframe by substring condition excluding similar strings

分類Dev

Match two strings based on common substring

分類Dev

Regex: How to remove a substring that is bounded by certain characters?

分類Dev

Regex: get number after and before specific substring

分類Dev

Creating a Regex expression with a “not” condition for a specific substring

分類Dev

Need to determine complete word from search that contains substring match

分類Dev

Objective-C - Works with strings, find a substring with NSRange

分類Dev

php substring occurances between two strings in an html file

分類Dev

php substring occurances between two strings in an html file

分類Dev

Substring in appinsight

分類Dev

Optimizing finding matching substring between the two lists by regex in Python

分類Dev

Apply conditional formatting to specific substring matches from REGEX

分類Dev

Extracting substring from PDF raw text using regex

分類Dev

Create new pd.DataFrame column if value of existing column contains specific substring

Related 関連記事

  1. 1

    Ramda, check(filter) if array of strings contains substring

  2. 2

    Get substring using regex

  3. 3

    Regex multiple match substring

  4. 4

    regex match substring unless another substring matches

  5. 5

    Check if String contains substring in clojure

  6. 6

    JScript regex - extract a substring between 2 words containing 1 or more occurrences of another word

  7. 7

    Regex: Getting Variable from Substring

  8. 8

    Javascript Extract from substring regex

  9. 9

    Replace strings in a string by a substring of those strings

  10. 10

    Filter an array of objects by a property containing a substring in AngularJS

  11. 11

    How to check whether a string contains a substring in Kotlin?

  12. 12

    Pandas: If a row contains this substring, update a cell in this row

  13. 13

    Look for file in directory that contains a certain substring

  14. 14

    use lodash to find substring from array of strings

  15. 15

    Check list of strings for substring, case-insensitive

  16. 16

    Sort dataframe by substring condition excluding similar strings

  17. 17

    Match two strings based on common substring

  18. 18

    Regex: How to remove a substring that is bounded by certain characters?

  19. 19

    Regex: get number after and before specific substring

  20. 20

    Creating a Regex expression with a “not” condition for a specific substring

  21. 21

    Need to determine complete word from search that contains substring match

  22. 22

    Objective-C - Works with strings, find a substring with NSRange

  23. 23

    php substring occurances between two strings in an html file

  24. 24

    php substring occurances between two strings in an html file

  25. 25

    Substring in appinsight

  26. 26

    Optimizing finding matching substring between the two lists by regex in Python

  27. 27

    Apply conditional formatting to specific substring matches from REGEX

  28. 28

    Extracting substring from PDF raw text using regex

  29. 29

    Create new pd.DataFrame column if value of existing column contains specific substring

ホットタグ

アーカイブ