• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

java regex

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am new to java 6 regex.

I have a string with the following content ( each line separated by \r\n).

Existing string:

aaaa|
TEST|x||y|z|""|cccc|bbb|"
xxxx|
TEST|x||y|z|Something|cccc|bbb|";
TEST|x|ST|y|z|""|cccc|bbb|";
TEST|x||y|z|""XYZ|cccc|bbb|";
TEST|x||y|z|Y""|cccc|bbb|";



The expected strign content:

aaaa|
TEST|x||y|z||cccc|bbb|
xxxx|
TEST|x|ST|y|z|Something|cccc|bbb|
TEST|x|ST|y|z|""|cccc|bbb|
TEST|x|ST|y|z|""XYZ|cccc|bbb|
TEST|x|ST|y|z|Y""|cccc|bbb|


Basically, if the content between 2nd and 3rd occurance of | is blank and if the content betweeen 5th and 6th occurance of | is "", then make it blank.(Ex: Line 2 in the EXISTING STRING)
Also if the content between 5th and 6th occurrence is not empty and not "" then if the content between 2nd and 3rd occurance of | is blank make it ST.(eX: Line 4,6,7 in existing string)
In other scenarios, nothing needs to be changed.
How to make it possible with Regex replaceall or Pattern/match.

I tried few things but in vain. - str.replaceAll( "(TEST\\\\|[\\|\\|]*\\\\|\\\\|[\\|\\|]*\\\\|[\\|\\|]*\\\\|)\"\"\\|", "$1\\|" ); - I t does not work for 4,6,7 lines/scenarios.

Please help as I have a dealine to meet.

Thanks a ton in advance.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rehash and virtually a double post of https://coderanch.com/t/511294/java/java/java-regex (only replacing the caret with the pipe character).

edit BeForthrightWhenCrossPostingToOtherSites
http://www.java-forums.org/advanced-java/32834-regex-search-replace.html
 
joe nesbitt
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any help please!

 
joe nesbitt
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to check if the content between 5th and 6th occurence of | has alphanumeric content and then check if the content between 2nd and 3rd | is empty, if so, make it STR.


My current regex is replacing all empty positions with STR. But I need it to change it only between 2nd and 3rd position.

Regex I am using:
(STR\\|[\\|\\|]*\\\\|)\\|\\|", "$1|STR|" );

The current (wrong output):

TEST|x|STR|y|z|Y""|cccc|bbb|STR|"

Any help is appreciated.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic