【问题标题】:java regex get value after specific valuejava 正则表达式在特定值后获取值
【发布时间】:2021-02-09 03:40:14
【问题描述】:

我有一些端点包含/bot/{idBot},我需要提取idBot

我们可以通过 java 中的正则表达式来实现吗?

以下是一些端点示例:

/bot/6/block/30/content/text
/bot/6/block/content/input/list/option/30
/account/bot/32/language
/account/bot/6

尝试过没有成功!

Matcher m2 = Pattern.compile("/bot/(\\d+)").matcher(path);

编辑:我有一些这样的端点:

/account/checkToken
/account/checkUser

那些需要被忽略的!

谢谢!!

【问题讨论】:

    标签: java regex pattern-matching endpoint


    【解决方案1】:

    您可以将String#replaceAll 用于正则表达式单行选项:

    String input = "/account/bot/32/language";
    String num = input.matches(".*/bot/\\d+.*") ? input.replaceAll(".*/bot/(\\d+).*", "$1") : "";
    System.out.println(num);
    

    【讨论】:

    • 感谢工作!!!但是嗯,我忘记了我可以拥有像这样的其他端点... /account/checkToken 例如,这给了我错误。
    • @sealabr 然后在/bot/ 没有出现在输入中的情况下使用String#matches 和三元表达式来分配空字符串。
    【解决方案2】:

    matcher.group(1) 就是你要找的。​​p>

    查看下面的完整代码。

    示例代码:

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    final String regex = "\\/bot\\/(\\d+)";
    final String string = "/bot/6/block/30/content/text\n"
         + "/bot/6/block/content/input/list/option/30\n"
         + "/account/bot/32/language\n"
         + "/account/bot/6";
    
    final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
    final Matcher matcher = pattern.matcher(string);
    
    while (matcher.find()) {
        System.out.println("Full match: " + matcher.group(0));
        System.out.println("Required match: " + matcher.group(1));
    }
    

    输出:

    Full match: /bot/6
    Required match: 6
    Full match: /bot/6
    Required match: 6
    Full match: /bot/32
    Required match: 32
    Full match: /bot/6
    Required match: 6
    

    编辑:为了支持排除某些 API,更新了正则表达式

    添加了负前瞻

    public static void main(String[] args) {
        final String regex = "^(?!\\/account\\/checkToken)(?!\\/account\\/checkUser).*\\/bot\\/(\\d+)";
        final String string = "/bot/6/block/30/content/text\n"
                + "/bot/6/block/content/input/list/option/30\n"
                + "/account/bot/32/language\n"
                + "/account/bot/6\n"
                + "/account/checkToken/bot/6\n"
                + "/account/checkUser/bot/78";
    
        final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
        final Matcher matcher = pattern.matcher(string);
    
        while (matcher.find()) {
            System.out.println("Full match: " + matcher.group(0));
            System.out.println("Required match: " + matcher.group(1));
        }
    }
    

    输出:

    Full match: /bot/6
    Required match: 6
    Full match: /bot/6
    Required match: 6
    Full match: /account/bot/32
    Required match: 32
    Full match: /account/bot/6
    Required match: 6
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 2020-05-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多