Java模式匹配用于匹配特定模式

标签 java regex split

我尝试匹配给定字符串中的静态模式,以下是我的程序:

package com.test.poc;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTestPatternMatcher {
  public static final String EXAMPLE_TEST = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}";

  public static void main(String[] args) {
    Pattern pattern = Pattern.compile("{\\w+}");
    // In case you would like to ignore case sensitivity you could use this
    // statement
    // Pattern pattern = Pattern.compile("\\s+", Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(EXAMPLE_TEST);
    // Check all occurance
    while (matcher.find()) {
      System.out.print("Start index: " + matcher.start());
      System.out.print(" End index: " + matcher.end() + " ");
      System.out.println(matcher.group());
    }
    // Now create a new pattern and matcher to replace whitespace with tabs
    Pattern replace = Pattern.compile("\\s+");
    Matcher matcher2 = replace.matcher(EXAMPLE_TEST);
    System.out.println(matcher2.replaceAll("\t"));
  }
} 

我尝试匹配 {} 中可用的字符串并将其替换为某个值。

但它给了我这个异常(exception):

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition
{\w+}
    at java.util.regex.Pattern.error(Pattern.java:1713)
    at java.util.regex.Pattern.closure(Pattern.java:2775)
    at java.util.regex.Pattern.sequence(Pattern.java:1889)
    at java.util.regex.Pattern.expr(Pattern.java:1752)
    at java.util.regex.Pattern.compile(Pattern.java:1460)
    at java.util.regex.Pattern.<init>(Pattern.java:1133)
    at java.util.regex.Pattern.compile(Pattern.java:823)
    at com.test.poc.RegexTestPatternMatcher.main(RegexTestPatternMatcher.java:9)

我在这里可能会遇到什么问题。很抱歉在这里问这个问题

最佳答案

{} 是保留字符。 你需要逃避这些: \\{\\}

仅供引用,这些字符用于重复。

http://www.regular-expressions.info/repeat.html

编辑

我不相信你可以使用这种匹配方式进行简单的替换。 您可以做的是构建一个新字符串,连续查找每个匹配:

public static void main( String[] args ) {
    String toConvert = EXAMPLE_TEST;

    Pattern pattern = Pattern.compile("\\{\\w+\\}");
    // In case you would like to ignore case sensitivity you could use this
    // statement
    // Pattern pattern = Pattern.compile("\\s+", Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(toConvert);
    StringBuilder resultStringBuilder = new StringBuilder();
    int startPos = 0;
    while (matcher.find()) {

        // append everything up to this match.
        resultStringBuilder.append(toConvert.substring(startPos, matcher.start()));

        // append the replacement
        resultStringBuilder.append(lookup(matcher.group()));

        // set the start pos for the next match
        startPos = matcher.end();
    }
    // append everything that's left.
    resultStringBuilder.append(toConvert.substring(startPos, toConvert.length()));        


    String resultStrig = resultStringBuilder.toString();
    System.out.println(resultStrig);
}


private static String lookup( String s ) {
    // decide what you want to replace this string with
    // You might want to make use of a TreeMap<String,String> here.
    return "";
}

关于Java模式匹配用于匹配特定模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16338000/

相关文章:

awk - 找到与第一列相同的列中的字母并打印它

java - JFace 给 SWT 添加了什么?

java - 数据存储计数查询 fetchOptions

c++ - std::regex_replace 给了我意想不到的结果

c# - 正则表达式仅用于数字加上 'h' 字符

javascript - 分割函数的结果是每次给我一个字符而不是单词

python - 如何在Python中将一个字符串分成10个字母?

java - 如何使用Java程序将数据加载到hive中?

java - 点击推送消息后访问推送通知内容(Android)

java - 在 Java 中按字面替换正则表达式中的字符