jhnclvr
jhnclvr

Reputation: 9487

How to find a String not surrounded by pipes in a pipe delimited file?

I am trying to find a regex that will match any pair of double quotes, that does not have exactly one pipe in front of it, and one pipe after it.

Here is the example line:

1||1|0|0|1|0|""|0|0|||""|""Billy""|"Johnson"|

In this line I would want to match the pairs of double quotes in front of and behind the word Billy.

The regex I tried is:

[^|]""[^|]

Which seems straightforward enough, but fails to match how I would think.

Is there a regex that will match those?

(Specifically I will be trying to replace those quotes in Java using String.replaceAll(), but a general regex solution would be fine at getting me started in the right direction.)

Upvotes: 0

Views: 111

Answers (2)

Daniel Bragg
Daniel Bragg

Reputation: 1953

You will need to escape the | symbol, because it has special meaning in RegEx.

Try [^\|]""[^\|] instead. You can verify this using online tools, like the one at http://www.roblocher.com/technotes/regexp.html

However, building off of anubhava's suggestion, you're looking for matches on the Quotes only, not the text in-between. Try this instead:

(?<!\|)""|""(?!\|)

It will match a string where there is not a | before the "", OR where there is not a | after the ""

Upvotes: 2

anubhava
anubhava

Reputation: 785008

You can use this regex:

(?<!\|)"[^|"]+"(?!\|)

RegEx Demo

Upvotes: 2

Related Questions