If element begins with a number, don't select the ending number

Viewed 28

I'm trying to select text for Dolby Digital channels, abbreviated DD, DDP, DD+, etc.

The problem I have is if the element already starts with 5.1 or 5, (decimal or int), I don't want it to select any numbers after it, because it could accidentally select numbers that are part of a different element.

https://regex101.com/r/MNq3Mw/2


Regex

\b\040?(\d[.]?\d?\040?)?(DDP|DD\+?\s?)([. ]?\040?\d[.]?\d?)?\b

I'm using \040 as whitespace, but I'd rather use \s, but it's selecting multiple lines.

I've tried lookahead and lookbehinds with no success.


Want All Selected

DDP 5.1
DD+ 5.1
DD 5.1
DDP5.1
DD+5.1
DD5.1

Want All Selected

5.1 DDP 
5.1 DD+
5.1 DD
5.1DDP
5.1DD+
5.1DD

Don't want the 5.1 ending selected

5.1 DDP 5.1
5.1 DD+ 5.1
5.1 DD 5.1
5.1DDP5.1
5.1DD+5.1
5.1DD5.1

Don't want the ending single number 5 selected

5.1 DDP 5
5.1 DD+ 5
5.1 DD 5
5.1DDP5
5.1DD+5
5.1DD5
1 Answers

You can use an alternation matching either or the formats:

\b(?:DDP|DD\+?)\s?5\.1|5\.1\s?(?:DDP|DD\+?)
  • \b Word boundary
  • (?: Non capture group
    • DDP|DD\+? Match either DPP or DD and optional +
  • ) Close group
  • \s?5\.1 Match opitonal whitespace char and 5.1
  • | Or
  • 5\.1\s? Match 5.1 and optional whitespace char
  • (?: Non capture group
    • DDP|DD\+? Match either DPP or DD and optional +
  • ) Close group

Regex demo

With decimal or int:

\b(?:DDP|DD\+?)\s?\d+(?:\.\d+)?|\d+(?:\.\d+)?\s?(?:DDP|DD\+?)

Regex demo

Related