Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

5 of 5
edited body
AGdev
  • 616
  • 8
  • 12

Java String regex replace

My string structure is following

characters 1-3 are uppercase alphabets including diacritics such Ň Ö Ï

Characters 4-7 will always be numbers.

8th is space

9th is forward slash

10th space

11th onwards are number.

String str1 = "DIW785o / 42"; // expected result "DIW7850 / 42"
String str2 = "QLR357Ï / 11"; // expected result "QLR3571 / 11"
String str3 = "UÜÈ7477 / 00"; // expected result "UÜÈ7477 / 00"
String str4 = "A / P8538 / 28"; // expected result "AÏP8538 / 28"
String str5 = "CV0875Z / 01"; // expected result "CVO8752 / 01"
String str6 = "SW / 2188 / 38"; // expected result "SWÏ2188 / 38"

I wanted replace first 3 characters such as

replaceAll("[2]", "Z")
.replaceAll("[0]", "O")
.replaceAll("[5]", "S")
.replaceAll(" // ","Ï) // replace space forward_slash space with Ï

and position where numbers with following

 .replaceAll("(?i)L|(?i)I", "1")
 .replaceAll("(?i)o", "0")
 .replaceAll("(?i)s", "5")
 .replaceAll("(?i)z", "2") 
AGdev
  • 616
  • 8
  • 12
lang-java

AltStyle によって変換されたページ (->オリジナル) /