Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
    Bookmark Topic Watch Topic
  • New Topic

java v6 regex question

 
joe nesbitt
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi all,

I am new to java regex.

I have a string that has 10 lines that contains three occurences of the word 'TEST' at the beginning of some of the new lines. Each of the occurrence of the word TEST 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:



Expected string:


How do I do it.

Any help is highly appreciated.


Thanks in advance.

 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Report post to moderator
What have you tried so far? What part of it isn't working?
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Report post to moderator
joe nesbitt wrote: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

Not to put too fine a point on it, that's as clear as mud. Moreover, only one of your sample inputs is shown to match the two or three conditions you have stated. You need more specific requirements ,and you need to post samples that match and don't match in various ways.

For TEST^ at the start of the line, use a positive zero-width lookbehind.For the rest, you need to be a lot more clear.
 
joe nesbitt
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Report post to moderator
ok...sorry for not the original post not be too clear.

Hope the below details are more clear and explains what I tried so far.

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 but not case 2 and 3.

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")

Any help is highly appreciated.

Thanks in advance.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Report post to moderator
joe nesbitt wrote:1a) I have a string that contains three occurences of the word 'TEST'.

Is TEST always preceded by newline?
 
joe nesbitt
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi

Thanks for the quick reply.

In the string, each line ends with a new line - \n. Some of the lines start with TEST (which is I am after).

The string example:

aaaa^ \n
TEST^x^^y^z^""^cccc^bbb^\n
bbb
TEST^x^This is not empty so ignore the content between 5th and 6th occurence of the search cahracter^y^z^""^cccc^bbb^\n
TEST^x^This is empty and since the content between 5th and 6th occurence of the search cahracter is NOT EMPTY, Please replace this with STR^y^z^NOT EMPTY^cccc^bbb^\n

Hope this helps.

Your help is highly appreciated as I have a deadline to meet for this task.

Thanks a ton in advance.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Report post to moderator
would you like an answer here or on your other thread?
http://www.coderanch.com/t/511457/java/java/java-regex
 
Campbell Ritchie
Sheriff
Posts: 48917
58
  • Mark post as helpful
  • send pies
  • Report post to moderator
Darryl Burke wrote:would you like an answer here or on your other thread? . . .
He can have it on the other thread because I am closing this thread.
 
    Bookmark Topic Watch Topic
  • New Topic