Home > Back-end >  Regex : Ass subtitles - Match the last \b tag value
Regex : Ass subtitles - Match the last \b tag value

Time:05-11

I want to match the numeric values of the last \b tag, or empty string if there's no number.

Here are the strings at left, and what I want to match at right :

\b                     Empty string
\b\b                   Empty string
\b1\b                  Empty string
\b\b1                  1
\b\b\i1                Empty string
\b1\blur0              1
\b2\b10anana           10
\b2\b1 0anana          1
\b2\bbanana10          Empty string
  • B tags with numeric value should return an empty string
  • In ass language the only tags starting with the b letters are \blur \bord and \be
  • There can be spaces between \b and the number but not after the number, hence why \b2\b1 0anana should give "1"
  • Obviously bbanana is not a real ass tag, but ass libraries consider tags starting by \b as the \b tag as long as it's not blur/bord/be.
  • Also, important precision : the \b tag can be "\ b" or "\b " (with as many spaces as possible)
  • I use a regex module that work like .NET (C#) regex in regex101

I'm currently stucked with this regex https://regex101.com/r/0TZeSn/1

(?<=\\\s*b\s*)\d (?!(?=\\b))|(?<=\\b)(?!.*\\b).*?(?=\w|\\)

It's hard to come by

CodePudding user response:

For a match only without empty matches, you might use:

(?<=\\ *b *)(?!\S*\\b *(?!lur|ord|e))\d 

Explanation

  • (?<=\\ *b *) Positive lookbehind, assert \b with optional spaces in between to the left
  • (?! Negative lookahead, assert not to the right
    • \S*\\b *(?!lur|ord|e) Match optional non whitespace chars followed by \b that is not directly followed by lur ord or e
  • ) Close lookahead
  • \d Match 1 digits

See a regex demo.

To also get empty matches, you can match optional digits and assert not lur ord or e directly after the current position and also not directly after matching \b

(?<=\\ *b *)(?!lur|ord|e|\S*\\b *(?!lur|ord|e))\d*

See another regex demo.

  • Related