0

I have a string that I would like to replace using a regular expression in java but I am not quite sure how to do this.

Let's say I have the code below:

String globalID="60DC6285-1E71-4C30-AE36-043B3F7A4CA6";
String regExpr="^([A-Z0-9]{3})[A-Z0-9]*|-([A-Z0-9]{3})[A-Z0-9]*$|-([A-Z0-9]{2})[A-Z0-9]*"

What I would like to do is apply my regExpr in globalID so the new string will be something like : 60D1E4CAE043; I did it with str.substring(0,3)+.... but I was wondering if I can do it using the regexpr in java. I tried to do it by using the replaceAll but the output was not the one I describe above.

To be more specific , I would like to change the globalID to a newglobalID using the regexpr I described above. The newglobalID will be : 60D1E4CAE043.

Thanks

  • Assuming no validation is required 5 simple substring calls seems the simplest way to do this. – Alex K. Sep 07 '17 at 10:57
  • Possible duplicate of [Java; String replace (using regular expressions)?](https://stackoverflow.com/questions/632204/java-string-replace-using-regular-expressions) – SpaceTrucker Sep 07 '17 at 10:58
  • It is not clear for what you want do this. But if you really need it, see https://stackoverflow.com/questions/1277157/java-regex-replace-with-capturing-group. – egorlitvinenko Sep 07 '17 at 10:58
  • @AlexK. Yeah I already did this but I was trying to do something more complex. Thanks – JustCodingPlease Sep 07 '17 at 10:58
  • As it stands you would could simply match the parts outside the segments you want and replace them with "" – Alex K. Sep 07 '17 at 11:00
  • @SpaceTrucker I saw this post but it not quite help me. I want to take the globalID, apply the regexpr on this string and then produce a new string with the format I describe. This is not what the post you mention indicates. – JustCodingPlease Sep 07 '17 at 11:01
  • @JustCodingPlease Then this might be a duplicate of https://stackoverflow.com/questions/17969436/java-regex-capturing-groups – SpaceTrucker Sep 07 '17 at 11:04

3 Answers3

0

Your regexp must match the whole string. Your wersioe tries to match the parts alternatively which does not work.

thy this:

String regExpr="^([A-Z0-9]{3})[^-]*"+
               "-([A-Z0-9]{2})[^-]*"+
               "-([A-Z0-9]{3})[^-]*"+
               "-([A-Z0-9]{2})[^-]*"+
               "-([A-Z0-9]{2}).*"
Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51
0

This is definitively not the best code ever, but you could do something like this:

String globalID = "60DC6285-1E71-4C30-AE36-043B3F7A4CA6";
String regExpr = "^([A-Z0-9]{3})[A-Z0-9]*|-([A-Z0-9]{3})[A-Z0-9]*$|-([A-Z0-9]{2})[A-Z0-9]*";

Pattern pattern = Pattern.compile(regExpr);
Matcher matcher = pattern.matcher(globalID);
String newGlobalID = "";
while (matcher.find()) {
    for (int i = 1; i <= matcher.groupCount(); i++) {
        newGlobalID += matcher.group(i) != null ? matcher.group(i) : "";
    }
}
System.out.println(newGlobalID);

You will need to use a Matcher to iterate over all matches in your input as your regular expression matches subsequences of the input string only. Depending on which substring is matched a different capturing group will be non-null, you could also use named capturing groups or remember where in the input you currently are, but the above code should work as example.

dpr
  • 10,591
  • 3
  • 41
  • 71
  • Great! This is exactly what I wanted. I tried to use the regexpr as such and didn't use the pattern and matcher! Thank you very much for you help! – JustCodingPlease Sep 07 '17 at 11:15
  • @JustCodingPlease, why did you unaccept the answer again? This approach is far more generic as it is not tied to a fixed number of sub-sequences in the id... – dpr Sep 07 '17 at 14:53
  • I did it by mistake!Wanted to upvote the guy above! My bad – JustCodingPlease Sep 07 '17 at 15:24
0

The total code should be like that below,

String globalID = "60DC6285-1E71-4C30-AE36-043B3F7A4CA6";
    String regExpr = "^(\\w{3}).*?-"
            + "(\\w{2}).*?-"
            + "(\\w{2}).*?-"
            + "(\\w{2}).*?-"
            + "(\\w{3}).*";
    System.out.println(globalID.replaceAll(regExpr, "$1$2$3$4$5"));

The output of println function is

60D1E4CAE043
AYRM1112013
  • 309
  • 1
  • 7