How to find all occurrences of a sequence of characters when a certain string is preceded?

I am trying to extract all the mappings from an EBML definition, something like this:

| + A track
|  + Track number: 3
|  + Track UID: 724222477
|  + Track type: subtitles
...
|  + Language: eng
...
| + A track
|  + Track number: 4
|  + Track UID: 745646561
|  + Track type: subtitles
...
|  + Language: jpn
...

I need all occurrences of "Language: ???" when "Track Type: Subtitles" precedes. I tried several variations of this:

Track type: subtitles.*Language: (\w\w\w)

I use a multi-line modifier in Ruby to match newlines (for example, the 's' modifier in other languages).

This works to get the last event , which in the example above will be "jpn", for example:

string.scan(/Track type: subtitles.*Language: (\w\w\w)/m)
=> [["jpn"]]

As a result, I would like to:

=> [["eng"], ["jpn"]]

What would be the correct regular expression to accomplish this?

+3
source share
2 answers

, :

.*

:

.*?

Track type: subtitles Language: (\w\w\w). , .

+7

.*. :

/Track type: subtitles.*?Language: (\w\w\w)/m

"Language: ???" "Track type: subtitles:". , - ( subtitles) Language.


:

/^\| \+ (?:(?!^\| \+).)*?\+  Track type: subtitles$(?:(?!^\| \+).)*?^\|  \+ Language: (\w+)$/m

, .


, :

/^\| \+ ([^\r\n]+)|^\|  \+ Track type: (subtitles)|^\|  \+ Language: (\w+)/m

( )

, . .

  • , . .
  • , subtitles.
  • , .
  • , subtitles, .
+3

Source: https://habr.com/ru/post/1699184/


All Articles