Home > Net >  Regular expression in java to parse a string ,split and recognize
Regular expression in java to parse a string ,split and recognize

Time:03-27

I am trying to breakdown a syntax expression when I encounter the below phrase using regex or split function.

Word:7W12345A or 7W12345B or 7W12345C or any char at the end like 7W(5digits)[A-Z]; whenever we encounter this string, I would need to convert into 7ABW1245, essentially add AB in between 7W and get rid of the char at the end.

Is there any easier way to perform this string manipulation with or without regex parsing?

I am using the context (global string variables in talend using tjava to perform the operation).

CodePudding user response:

You can do it like this.

  • (\\d{5}) - captures the digits (assuming they could be different)
  • $1 back references that capture.
String[] str = {"7W12345A","7W12245B","7W12422C"};
for (String s : str) {
    String st = s.replaceAll("7W(\\d{5})[A-Z]", "7ABW$1");
    System.out.println(s   " --> "   st);
}

prints

7W12345A --> 7ABW12345
7W12245B --> 7ABW12245
7W12422C --> 7ABW12422

String word = 7W12822Z";
word = word.replaceAll("7W(\\d{5})[A-Z]", "7ABW$1");
System.out.println(word);

prints

7ABW12822

CodePudding user response:

Assuming that you are referring to strings starting with 7W and ending with A, B, C, or something else you can you something like this.

if( Stream.of( "A", "B", "C" ).anyMatch( word::endsWith ) )
{
    word = word.replaceFirst( "7W", "7ABW" ).replaceAll( ".$", "" );
}

Above matches the ending character with A, B, or C and replaces the first 7W with 7ABW and removes the last charactor.

  • Related