1

My current regex query is

    (?P<TestName>(?<=_)[^\s]+)

Its able to capture "ABCD" and "Adjust_Path". I would like to capture the string "LED" as well from the list below

    4039_ABCD
    LED
    2020_Adjust_Path
Don99
  • 209
  • 1
  • 2
  • 9

2 Answers2

0
(?<=_)[^\s]+)|((?!.*_)[^\s]+)

| : for the 2 cases (alternatives)

(?!.*_) : second case : not every string with a _

More info

Regular expression with if condition

Dri372
  • 1,275
  • 3
  • 13
0

You might assert an underscore to the left, and match any char except a whitespace char or underscore till the end of the string.

(?P<TestName>(?<=_)\S+|[^\s_]+)$

The pattern matches:

  • (?P<TestName> Named group TestName
    • (?<=_) Positive lookbehind, assert _ directly to the left
    • \S+ Match 1+ times a non whitespace char
    • | Or
    • [^\s_]+ Match 1+ times any char except _ or a whitespace char
  • ) Close group TestName
  • $ End of string

See a regex demo.

If there has to be a digit present before the underscore:

(?P<TestName>(?<=\d_)\S+|[^\s_]+)$
The fourth bird
  • 154,723
  • 16
  • 55
  • 70