tags:

views:

95

answers:

2

Hi all,

Couple of questions:

1) How to make the following regex which are based on search literal ^ work for the search literal |

search literal ^ based regex (which works fine, which is one of the valuable inputs from this forum):

String intermediateResult = in.replaceAll( "(TEST\\^[^^]*\\^\\^[^^]*\\^[^^]*\\^)\"\"\\^", "$1^" );
String finalResult = intermediateResult.replaceAll( "(TEST\\^[^^]*\\^)(\\^[^^]*\\^[^^]*\\^([^\"\\^].*|\"[^\"].*))", "$1ST$2" );

When I replace ^ (where ever required) to | as follows - I do not get the desired result(it does not change anything in the given string):

String intermediateResult = in.replaceAll( "(TEST\\|[\\|\\|]*\\|[\\|\\|]*\\|[\\|\\|]*\\|[\\|\\|]*\\|)\"\"\\|", "$1|" );
String finalResult = intermediateResult.replaceAll( "(TEST\\|[\\|\\|]*\\\\|)(\\|[\\|\\|]*\\|[\\|\\|]*\\|([^\"\\^].*|\"[^\"].*))", "$1ST$2" );

Are there any known issues with | in java regex or do I need to have the regex differently for search literal |

So I tried this way but in vain (Having \\| instead of \|):

First regex changes all places that are like |""| in the given string, though I expect it to make it blank only if the content between 5th and 6th occurence of | is "", not sure why. The second regex does not change the string at all for some reason.

 String intermediateResult = in.replaceAll( "(TEST\\|[\\|\\|]*\\\\|[\\|\\|]*\\\\|[\\|\\|]*\\\\|[\\|\\|]*\\\\|)\"\"\\|", "$1|" );
String finalResult = intermediateResult.replaceAll( "(TEST\\|[\\|\\|]*\\\\|)(\\\\|[\\|\\|]*\\\\|[\\|\\|]*\\|([^\"\\^].*|\"[^\"].*))", "$1ST$2" ); 

2) Also what does the match part and replacement str of this regex imply:

String finalResult = intermediateResult.replaceAll( "(TEST\\^[^^]*\\^)(\\^[^^]*\\^[^^]*\\^
**([^\"\\^].*|\"[^\"].*)**)", "**$1ST$2**" );

Any help is highly appreciated

Thanks in advance.

A: 

I found one error in your conversion. The pattern [^^] means "a character, that is not a '^'". Thus, to convert it for "|", it would be [^|] (inside of [], no escape is needed for |).

Now I'm going to take a fork and stab my eyes out. I never want to see that again.

Arian
Thanks for your input! Sorry for the long regex. So is the correction you mentioned is in the second regex - String finalResult = intermediateResult.replaceAll(xxxxx) mentioned in 2nd code snippet? Thanks again!
joe robles
Yes, replace all [\\|\\|] with [^|].
Arian
Thanks. I made the change but ST is not placed in the desired position when tested. The changes one is as follows:
joe robles
intermediateResult.replaceAll( "(TEST\\|[^|]*\\|)(\\|[^|]*\\|[^|]*\\|([^\"\\^].*|\"[^\"].*))", "$1ST$2" );
joe robles
One more question - How to make first line in 2nd code snippet - in.replaceall(xxxx) to work coreectly so that it changes "" to blank only in the 5th occurence and no change "" that are in different positions on the same line. Basically, it needs to change "" to blank if present between 5th and 6th occurence on each line that starts with TEST. I tried with replaceFirst() but in vain. I guess the regex pattern need to be restricted to the "" that is found between 5th and 6th occurence of | on each line. I am new to regex. Hope to get some help regarding this. Thx in advance.
joe robles
A: 

Extremely sorry for the confusion and the long post; I am going to post only one question and post the other question in a different post for clarity sake.

Eloborating ont he first question fromteh originall mail:

I have the following string (each line is seperated by \r\n). The first regex related to search literal | is supposed to check if the content between second and 3rd occurence of | is blank and if content between 5th and 6th | is "", then make it blank.

2nd regex (again related to |) must see if the content between 5th and 6th occurence of | is not empty and not null, then make the content between second occurenace of 2nd and 3rd as ST.

Example as follows:

Existing string:

TEST|X||Y||**""**|C|""|\r\n\
TEST|Z||Y||SOMETHING OTHER THAN "" OR empty||\r\n\

Desirted output when the | related two regex replaceall() from the original posting are run:

TEST|X||Y|||C|""|\r\n\
TEST|Z|**ST**|Y||SOMETHING OTHER THAN "" OR empty||\r\n\
joe robles