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.
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 whitespacesE[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.