Reputation: 29
Aim:
Capture everything up until a character sequence & if those character sequences don't exist, capture entire text.
Character sequences:
| table &
| stats
Ex 1:
search foo bar | table _time Action Direction
returns search foo bar
Ex 2:
search foo bar hello hi ciao 1234
returns search foo bar hello hi ciao 1234
Ex 3:
search foo bar
bar foo
| table _time Action Direction
returns
search foo bar
bar foo
(?<var>(?s).+?(?=\Q| table\E|\Q| stats\E))
Upvotes: 2
Views: 976
Reputation: 626689
You can use
(?s)^(?<var>.*?(?=\| (?:table|stats)|$))
See the regex demo.
Details:
(?s)
- singleline/dotall modifier on^
- start of string(?<var>.*?(?=\| (?:table|stats)|$))
- Group "var":
.*?
- zero or more chars, as few as possible, up to the leftmost occurrence of(?=\| (?:table|stats)|$)
- either |
+space+table
or stats
, or end of string.Upvotes: 1