pr338
pr338

Reputation: 9170

Extract a value between semicolons that has a word you want in regex

How can I use regex to pull a the following column titles out of a header list?

Case 1 - ;CASE_NUMBER;CASE_STATUS;CASE_SUBMITTED;... -> CASE_STATUS

Case 2 - ;LCA_CASE_NUMBER;STATUS;LCA_CASE_SUBMIT;... -> STATUS

Here is what I've tried:

  1. (?<=;)STATUS;

Positive Lookbehind (?<=;) Assert that the Regex below matches ; matches the character ; literally (case sensitive) STATUS; matches the characters STATUS; literally (case sensitive)

  1. (?<=;).*STATUS;

I added to include characters in between the ; and the word STATUS. I realize the current notation is "greedy", so I finally tried the next option.

  1. (?<=;).*?STATUS;

This is supposed be "lazy", meaning it should only grab everything after the first ; it encounters before STATUS. However, I am getting the same results when using 2 and 3 as a regex and both capture the words starting at the initial semi colon.

How do I extract the letters from the first semicolon behind STATUS to the first semi colon after STATUS?

Upvotes: 1

Views: 63

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627419

However lazy the .*? pattern is, the . can match any char (usually all but line break chars) including ; if the patterns after .*? can appear after a ;.

You may use

[^;]*STATUS[^;]*

See the regex demo

Details

  • [^;]* - 0+ chars other than ;
  • STATUS - your word
  • [^;]* - 0+ chars other than ;.

Upvotes: 2

Related Questions