简体   繁体   English

正则表达式获取除冒号之外的两个冒号之间的值

[英]Regex to get value between two colon excluding the colons

I have a string like this: 我有一个像这样的字符串:

something:POST:/some/path

Now I want to take the POST alone from the string. 现在我想从字符串中单独使用POST。 I did this by using this regex 我使用这个正则表达式做到了这一点

:([a-zA-Z]+):

But this gives me a value along with colons. 但这给了我一个冒号值。 ie I get this: 即我得到这个:

:POST:

but I need this 但我需要这个

POST

My code to match the same and replace it is as follows: 我的代码匹配相同并替换它如下:

String ss = "something:POST:/some/path/";
Pattern pattern = Pattern.compile(":([a-zA-Z]+):");
Matcher matcher = pattern.matcher(ss);

if (matcher.find()) {
    System.out.println(matcher.group());
    ss = ss.replaceFirst(":([a-zA-Z]+):", "*");
}
System.out.println(ss);

EDIT: 编辑:

I've decided to use the lookahead/lookbehind regex since I did not want to use replace with colons such as :*: . 我决定使用lookahead / lookbehind正则表达式,因为我不想使用冒号replace ,例如:*: . This is my final solution. 这是我的最终解决方案。

String s = "something:POST:/some/path/";
String regex = "(?<=:)[a-zA-Z]+(?=:)";

Matcher matcher = Pattern.compile(regex).matcher(s);

if (matcher.find()) {
    s = s.replaceFirst(matcher.group(), "*");
    System.out.println("replaced: " + s);
} 
else {
    System.out.println("not replaced: " + s);
}

There are two approaches: 有两种方法:

  • Keep your Java code, and use lookahead/lookbehind (?<=:)[a-zA-Z]+(?=:) , or 保留Java代码,并使用lookahead / lookbehind (?<=:)[a-zA-Z]+(?=:) ,或者
  • Change your Java code to replace the result with ":*:" 更改Java代码以使用":*:"替换结果

Note: You may want to define a String constant for your regex, since you use it in different calls. 注意:您可能希望为正则表达式定义String常量,因为您在不同的调用中使用它。

UPDATE UPDATE

Looking at your update, you just need ReplaceFirst only: 查看更新,您只需要ReplaceFirst

String result = s.replaceFirst(":[a-zA-Z]+:", ":*:");

See the Java demo 请参阅Java演示

When you use (?<=:)[a-zA-Z]+(?=:) , the regex engine checks each location inside the string for a * before it, and once found, tries to match 1+ ASCII letters and then assert that there is a : after them. 当你使用(?<=:)[a-zA-Z]+(?=:) ,正则表达式引擎会检查字符串中的每个位置是否为*之前的* ,并且一旦找到,就会尝试匹配1个以上的ASCII字母和然后声称有一个:在他们之后。 With :[A-Za-z]+: , the checking only starts after a regex engine found : character. 使用:[A-Za-z]+: ,仅在找到正则表达式引擎后才开始检查:字符。 Then, after matching :POST: , the replacement pattern replaces the whole match. 然后,匹配后:POST: ,替换模式替换整个匹配。 It is totlally OK to hardcode colons in the replacement pattern since they are hardcoded in the regex pattern . 在替换模式中对冒号进行硬编码是完全可以的,因为它们在正则表达式模式中是硬编码的

Original answer 原始答案

You just need to access Group 1: 您只需要访问第1组:

if (matcher.find()) {
    System.out.println(matcher.group(1));
}

See Java demo 请参阅Java演示

Your :([a-zA-Z]+): regex contains a capturing group (see (....) subpattern). 你的:([a-zA-Z]+):正则表达式包含一个捕获组 (见(....)子模式)。 These groups are numbered automatically: the first one has an index of 1 , the second has the index of 2 , etc. 这些组自动编号:第一个具有索引1 ,第二个具有索引2 ,等等。

To replace it, use Matcher#appendReplacement() : 要替换它,请使用Matcher#appendReplacement()

String s = "something:POST:/some/path/";
StringBuffer result = new StringBuffer();
Matcher m = Pattern.compile(":([a-zA-Z]+):").matcher(s);
while (m.find()) {
    m.appendReplacement(result, ":*:");
}
m.appendTail(result);
System.out.println(result.toString());

See another demo 另一个演示

As pointed out, the reqex captured group can be used to replace. 正如所指出的,可以使用reqex捕获的组来替换。

The following code did it: 以下代码执行了它:

  String ss = "something:POST:/some/path/";
    Pattern pattern = Pattern.compile(":([a-zA-Z]+):");
    Matcher matcher = pattern.matcher(ss);

    if (matcher.find()) {
        ss = ss.replaceFirst(matcher.group(1), "*");
    }
    System.out.println(ss);

This is your solution: 这是你的解决方案:

regex = (:)([a-zA-Z]+)(:) regex = (:)([a-zA-Z]+)(:)

And code is: 代码是:

String ss = "something:POST:/some/path/";

ss = ss.replaceFirst("(:)([a-zA-Z]+)(:)", "$1*$3");

ss now contains: ss现在包含:

something:*:/some/path/ 东西:*:/一些/路径/

Which I believe is what you are looking for... 我相信你正在寻找...

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

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