开发者

How can I match series of | with a Java regex?

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

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.

Elaborating on the first question from the original e-mail:

I have the following string (each line is separated by \r\n). The first regex related to search literal | is supposed to check if the content between second and 3rd occurrence 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 occurrence of | is not empty and not null, then make the content between second occurrence 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\

Desired 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\


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜