aspose file tools*
The moose likes Beginning Java and the fly likes java v6 regex question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply locked New topic
Author

java v6 regex question

joe nesbitt
Greenhorn

Joined: Dec 05, 2009
Posts: 17
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

Joined: Oct 01, 2001
Posts: 2864
    
  11

What have you tried so far? What part of it isn't working?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
joe nesbitt
Greenhorn

Joined: Dec 05, 2009
Posts: 17
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

Joined: May 03, 2008
Posts: 4664
    
    5

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

Joined: Dec 05, 2009
Posts: 17
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

Joined: May 03, 2008
Posts: 4664
    
    5

would you like an answer here or on your other thread?
http://www.coderanch.com/t/511457/java/java/java-regex
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java v6 regex question