Hi all,
I am new to java regex.
Sorry for the long posting.
I have a three requirements:
1a) 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 Example: Existing string:
aaaa^
TEST^x^^y^z^""^cccc^bbb^
Expected string:
aaaa^
TEST^x^^y^z^^cccc^bbb^
1b) If the content between 2nd and 3rd occurence of ^ is not blank and not "", then do not change the content between 5th anc 6th occurence
Existing string:
TEST^p^^q^r^""^lll^mmm^
Expected string:
TEST^p^^q^r^""^lll^mmm^
I need to repeat this logic check whenever TEST word is found.
1c) If the content between 5th and 6th occurence of ^ is not blank and not "" and if the content between 2nd and 3rd is blank/empty then replace it with STR.
Existing string:
TEST^g^^q^r^YYY^lll^mmm^
Expected string:
TEST^g^STR^q^r^YYY^lll^mmm^
I need to accomplish allt he above cases in a java regex. I could make case 1 work BASED ON valuable input from my previous posting in this forum. I could not make case 2 and 3 work.
How to accomplish case 2 and 3 in the same regex expression ( I am not sure as what the regex expressions are for not empty contnet check and for 'OR' check). In non regex world, in plain if else approach, I can take care of the 3 cases as follows:
if (the content between 2nd and 3rd occurence of ^ is empty)
{
if(content between 5th and 6th occurence of ^ is "")
{
make this content empty
}
else
{
set the content between 2nd and 3rd occurence of ^ as STR
}
}
But since I need to make this check for each line which starts with the word TEST in the String, I am leaning towards regex.
So far the regex that works for case 1 is as follows:
str.replaceAll("(TEST\\^[^^]*\\^\\^[^^]*\\^[^^]*\\^)\"\"", "$1")
For 2nd case, I tried modifying the above regex as follows but in vain(Tried to search for not empty VALUE BETWEEN 2ND AND 3RD OCCURENCE where i asSumed *\\d0$
represents EMPTY and [\\d0$]
implies NOT EMPTY):
str.replaceAll("(TEST\\^[^^]*\\^[^\\d0$]\\^[^^]*\\^[^^]*\\^)\"\"", "$1")
Any help to come up with regex expression that takes care of the above 3 usecases is highly appreciated as I have a deadline to meet for this task.
Any help is HIGHLY appreciated.
Thanks in advance.