Praphi
Praphi

Reputation: 33

regex to extract by be removing a pattern if present at the end

I am trying to extract value from field if containing "- E1/E2" and ignore the rest.

I have used (?<value>.*)\s+-\s+E[1-3]* but it doesn't extract if doesn't contain E1/E2.

below sample values. it doesn't work for 2,4 sample data. Unable to understand how do i make E1/E2 as optional.

  1. hello - I am here at - E1
  2. Kilo
  3. How are you - E3
  4. Cool
  5. simple - E1 - Dev
  6. another simple - E2 QA

Upvotes: 3

Views: 39

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627546

You can use

^(?<value>.*?)(?:\s+(?:-\s+)?E[1-3].*)?$

See the regex demo.

Details

  • ^ - start of string
  • (?<value>.*?) - Group "value": zero or more chars other than line break chars as few as possible
  • (?:\s+(?:-\s+)?E[1-3].*)? - an optional non-capturing group:
    • \s+ - one or more whitespaces
    • (?:-\s+)? - an optional sequence of - and one or more whitespaces
    • E[1-3] - E and a digit from 1 to 3
    • .* - zero or more chars other than line break chars as many as possible
  • $ - end of string.

Upvotes: 1

Related Questions