Find multiple matches starting with specific symbols and then consecutive occurrences of specific words and patterns

Viewed 34

I have the following kind of string "sometext1 §§ 12 Abs. 5, 13a, 14 Satz 1 Nr. 3, 9, 8 sometext2". I want to find a §§ substring and all consecutive occurrences of Abs., und, Satz and Nr. as well as digits with a single character like 13a.

Examples:

"Die Anzahl der §§ 12 Abs. 5, 13a, 14 Satz 1 und 8 kann variieren. Für die §§ 15a, 18 Abs. 5, 21 und 23 Satz 3 trifft dies nicht zu.

Here I want to get 12 Abs. 5, 13a, 14 Satz 1 und 8 and 15a, 18 Abs. 5, 21 und 23 Satz 3.

I used the following regex 'r'§§ (.*)? ^(?!Satz|Abs.|Nr.|\d+[a-z]| |,)'.

1 Answers

You can use

§§\s*((?:Satz|Abs\.|Nr\.|\d+[a-z]?|und|[\s,])+)(?<=\w)

See the regex demo. Details:

  • §§ - a literal text
  • \s* - zero or more whitespaces
  • ((?:Satz|Abs\.|Nr\.|\d+[a-z]?|und|[\s,])+) - Group 1 capturing one or more occurrences of Satz, Abs., Nr., one or more digits optionally followed by a lowercase ASCII letter, und, whitespace or comma.
  • (?<=\w) - the char immediately on the left must be a word char.
Related