Home > Enterprise >  Java regex question: How to match a sting from N-th field in a line using regex?
Java regex question: How to match a sting from N-th field in a line using regex?

Time:08-27

I am trying to match string "RAV" on every line on 15th word. Each word is delimited by "|" (pipe). I need to compare if line has this string or not. If yes, I will proceed with my next step.

I was trying this

((((?:[^|]*\|){17}(.*?)\|) \|[^|]*$)|[^|] (?=(\,\H\,))|(^([0-9]){4})|(([RAV]){3}))

But this one matches even if I have "RAV" first, second or any other fields.

I need to match exactly on 15th field.

CodePudding user response:

You could match the first 14 words, and then capture the 15th word after the pipe.

If you want to prevent a partial word, you can always append a word boundary (RAV)\b

^(?:\w \|){14}(RAV)

Regex demo

Or using a negated character class, but that can match more that 1 word between the pipes:

^(?:[^|] \|){14}(RAV)

Regex demo

CodePudding user response:

It is not returning this pattern. String is "8/23/2022 7:35:08 AM|33224455|DF My-Store|ABCD-OY-MMNNYYDD|98121928|DF|FNU LNU|1234 ISLAND COVE CT|DELTA LAKE|CT|98765-8470||73023|MIS|MIS VAR DISPLAY|RAV||MYDF'" As you see 15th field has string "RAV".

I need get this pattern "RAV" from 15th field. I tried following "'(^(?:[^|]|){15}([^|]) |[^|]*$)|[^,] (?=(,\H,))|(^([0-9]){4})|(([RAV]){3})'". This fetch pattern "RAV" from any field. That is the issue. I need to check 15th field specifically and then match it with "RAV"

let me know. Thanks

  • Related