简体   繁体   English

使用正则表达式替换字符串

[英]Replace a string using a regular expression

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. 我有一个要在Java中使用正则表达式替换的字符串,但我不太确定如何执行此操作。

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; 我想做的是将我的regExpr应用到globalID中,因此新字符串将类似于:60D1E4CAE043; I did it with str.substring(0,3)+.... but I was wondering if I can do it using the regexpr in java. 我是用str.substring(0,3)+ ....完成的,但是我想知道是否可以使用Java中的regexpr来做到这一点。 I tried to do it by using the replaceAll but the output was not the one I describe above. 我试图通过使用replaceAll来做到这一点,但是输出不是我上面描述的那样。

To be more specific , I would like to change the globalID to a newglobalID using the regexpr I described above. 更具体地说,我想使用上述的regexpr将globalID更改为newglobalID。 The newglobalID will be : 60D1E4CAE043. newglobalID将为:60D1E4CAE043。

Thanks 谢谢

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}).*"

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. 您将需要使用Matcher来迭代输入中的所有匹配项,因为您的正则表达式仅匹配输入字符串的子序列。 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. 根据匹配的子字符串,不同的捕获组将为非空,您也可以使用命名的捕获组或记住当前输入中的位置,但是上述代码应作为示例。

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 println函数的输出是

60D1E4CAE043

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM