tags:

views:

87

answers:

3

Hi all,

I am new to java regex.

I have a string that contains three occurences of the word 'TEST'. Each of this word is followed by ^ and I need to check if the content between 2 and and 3rd occurence of ^ is blank, if it is blank/empty search further to see if the content between the 5th and 6th occurence of the ^ is "" If it is "", then replace it to be blank/empty

I need to repeat this logic check whenever TEST word is found.

Example:

Existing string:

aaaa^
TEST^x^^y^z^""^cccc^bbb^
bbbb^
TEST^p^^q^r^""^lll^mmm^
TEST^P^X^q^n^""^hhh^ttt^

Expected string:

aaaa^
TEST^x^^y^z^^cccc^bbb^
bbbb^
TEST^p^^q^r^^lll^mmm^
TEST^NOT EMPTY THUS IGNORE^X^q^n^""^hhh^ttt^

How do I do it.

Any help is highly appreciated.

Thanks in advance.

A: 
String resultString = subjectString.replaceAll("(TEST\\^[^^]*\\^\\^[^^]*\\^[^^]*\\^)\"\"\^", "$1");

Explanation:

(       # start capturing group
 TEST   # match TEST
 \^     # match ^ (no. 1)
 [^^]*  # match 0 or more non-^ characters
 \^\^   # match ^^ (no. 2 and 3)
 [^^]*  # match 0 or more non-^ characters
 \^     # match ^ (no. 4)
 [^^]*  # match 0 or more non-^ characters
 \^     # match ^ (no. 5)
)       # end capturing group
""      # match ""
\^      # match ^ (no. 6)
Tim Pietzcker
Thanks!...I tried it. Unfortunatley nothing is changed in the modified string. I tried out your code with this:
joe robles
TEST^x^^y^z^""^cccc^bbb^
joe robles
and the resultant string is still as TEST^x^^y^z^""^cccc^bbb^
joe robles
I was expecting this - TEST^x^^y^z^^cccc^bbb^ Hope to know your input. Thanks.
joe robles
Sorry, it was nearly midnight when I posted this, so I couldn't react to your comments. The regex is correct (I amended it today as per @Hemang's suggestion, but that shouldn't have prevented it from matching). So I guess there is some other problem elsewhere.
Tim Pietzcker
Sorry for the confusion. It works fine. I have other requirements and for clarity sake I mentioned them at the below link. Is it possible to accomplish my requirements using regex (null chk/usage of AND/OR). Hope to know your valuable input...thanks a ton - http://stackoverflow.com/questions/3784464/java-regex-searching-for-empty-content-between-two-occurences-of-a-search-char
joe robles
Hi...hope to know your valuable input for my latest issue in the posting mentioned above. Basically, how to check for not-null checks and replacement only in the desired position when the search literal is | Thanks in advance.
joe robles
A: 

^ is the delimiter, so you could split the string into tokens:

String[] tokens = text.split("\\^", -1);

Then search through that array of tokens looking for TEST, applying whatever logic you need, updating the tokens as needed.

Finally, output the updated list of tokens, separated by ^:

for (int i=0; i<tokens.length; i++) {
    if (i > 0) {
        System.out.print("^");
    }
    System.out.print(tokens[i]);
}
System.out.println();

Doing it this way, the algorithm you are implementing will be much clearer.

Richard Fearn
A: 

Tim's answer is almost correct. However there can be case where after "" there is no 6th ^ but some other character. The correction is

String resultString = subjectString.replaceAll("(TEST\\^[^^]*\\^\\^[^^]*\\^[^^]*\\^)\"\"\\^", "$1^");
Hemang
Never mind ...got it to work for string with ^. Thanks! But I have one more requirement where the string is seperated with | instead of ^. In such cases, it is not working. Is | a special character in regex? How to make this work: TEST|x||y|z|""|cccc|bbb| The expected output must be TEST|x||y|z||cccc|bbb|. Thanks in advance.
joe robles
k. I got it to work for | by esacaping it - \\|
joe robles
For one more use case of my current task - Can the regex expression given by you be further used to do this - to replace "" the above example when the content between 2nd and 3rd | is not empty.
joe robles
.Example: Existing string: TEST|x|THIS IS NOT EMPTY THUS DO NOT CHANGE THE CONTENT BETWEEN 5TH AND 6TH OCCURENCE OF THE SEARCH CHAR|y|z|""|cccc|bbb| - No change expected when regex goes through this string. Thanks in advance!
joe robles
As I have one more requirement, for clarity sake, I used a new posting which covers all the requirements details and what I tried so far and issues so far. It is at the link below. Hope to know your valuable input as I have a deadline to meet. Thanks a ton in advance. the link - http://stackoverflow.com/questions/3784464/java-regex-searching-for-empty-content-between-two-occurences-of-a-search-char
joe robles