tags:

views:

74

answers:

2

Hi all,

I have a string as follows:

TEST|A||B|C|**""**|D|""|||||\r\n
TEST|Z||V|P|**""**|Y||||||||\r\n

I need to make the content between 5th and 6th occurence of | to blank if the content is "". So the desired output is

TEST|A||B|C||D|""|||||\r\n
TEST|Z||V|P||Y||||||||\r\n

So I am using matcher/pattern and matcher.replaceFirst() using this regex pattern:

String regexPattern = "TEST.*\\|([|\\|]*)\\|([|\\|]*)\\|([|\\|]*)\\|([|\\|]*)\\|\"\"\\|([|\\|]*)"

Though I want only the "" in the 5th and 6th occurence of | to be made EMPTY, the progarm is replacing all "" that it encounters to EMPTY.

So the output is as follows:

TEST|A||B|C||D||||||\r\n
TEST|Z||V|P||Y||||||||\r\n

I am not sure if I need to change the regex pattern that I have? OR is there a way to tell the matcher class to tell that once it changes the first "" do not change other "" in that matched pattern but change it in the other occurence of TEST fragment, If so how?

Any help is appreciated.

Thanks in advance.

A: 

Got it. Extremely sorry for the mega confusion. I will try my level best to make it precise/clear as much as possible.

Question rephrased:

I want to use a pattern to search in a string using java Pattern and matcher classes.

The string has two lines with data seperated by| and each line separated by \r\n\

Simplified example is as follows:

TestString content:

TEST|JUNK1|JUNK2|JUNK3|JUNK4|""|JUNK6|""|JUNK8
TEST|JUNK11|JUNK12|JUNK13|JUNK14|""|JUNK16|

I want "" which is after JUNK4| (the content between 5th and 6th occurence of | in the line) in the above example to be made empty/blank. I do not want "" in other positions of that line to change. Same thing needs to happen in the next line.

So I am using the following regex pattern in conjunction to Java Matcher and Pattern classes is as follows:

String regexPattern = "TEST.*\\|([|\\|]*)(\"\")([|\\|]*)\\|";

But it is replacing all occurences of "" in each line and this is not as per my requirement.

I tried quantifier/POSIX {1} but in vain thinking that it would restrict the search for the first "" in the TestStr but in vain.

Regex with {1} usage:

String regexPattern = "TEST.*\\|([|\\|]*)(\"\"){1}([|\\|]*)\\|";

So how to make the restriction in the pattern to get the fragment of the string till the first "".

Thanks in advance.

joe robles
Just so you know, there is an "Edit" feature for questions.
Jim Tough
A: 

A simple answer not using complex regexes:

String temp[] = inputString.split("\\|");
if ("\"\"".equals(temp[5]))
{
    temp[5] = "";
    StringBuilder buf = new StringBuilder();
    for (String s : temp)
        buf.append("|").append(s);
    inputString = buf.substring(1);
}

And also, pay attention to what others are saying to you. USE THE EDIT FACILITY, don't post corrections as answers.

Jim Garrison