使用正则表达式解析正则表达式

huangapple go评论72阅读模式
英文:

Parse regex with regex

问题

I have pattern [a-z0-9]{9}|[a-z0-9]{11}

这里,我需要解析这个正则表达式,以便在将来的消息中获取 9 和 11,用于提示“您需要输入 9 或 11 个符号”。

Also I have regex like

[a-z0-9]{9}

[a-z0-9]{9,}

[a-z0-9]{9,11}

我需要编写一个方法,可以解析所有这些字符串,以获取可用符号的数量。

我已经尝试过

Pattern.compile(".*\\{(\\d+)(,?)(\\d*)\\}(\\|.*\\{(\\d+)\\})?");

它只能找到第一个模式中的最后一个数字。但是其他的无法识别。

如何检查所有字符串?

更新:
我无法理解,但是在Java中,答案的第一个模式\{(\d+)(?:,(\d*))?\} 无法匹配,但可以找到我的三个字符串。但第一个和最长的字符串却找不到。

matches 和 find 之间有什么区别?
为什么该模式在网络中可以找到匹配项,但在Java中找不到?
有时我执行

matcher.find(); // 返回 2 matcher.group(1); // 抛出无匹配项异常

英文:

I have pattern [a-z0-9]{9}|[a-z0-9]{11}

Here, I have to parse that regex to get 9 and 11 for future message that "U have to input 9 or 11 symbols"

Also I have regex like

[a-z0-9]{9}

[a-z0-9]{9,}

[a-z0-9]{9,11}

I need to write some method,which can parse all that strings to get Number of available symbols.

I did

Pattern.compile(".*\\{(\\d+)(,?)(\\d*)\\}(\\|.*\\{(\\d+)\\})?");

It find only last number in my first pattern. But other don't recognize.

How to check all strings?

Upd:
I can not understand, but in Java first pattern from answer
\{(\d+)(?:,(\d*))?\}
doesn't match but find 3 my strings. But first and the longest string it doesn't find.

What is different between matches and find?
And why that pattern can find matches in web, but can't in Java?
And sometimes I do
matcher.find(); // return 2
matcher.group(1); // No match found exception

答案1

得分: 1

这个正则表达式[应该适用][2]于你:`[^\\](?:\\\\)*\{(\d+)(?:,(\d*))?\}`。

部分 `[^\\](?:\\\\)*` 处理了反斜杠转义的情况。

**以下是 Java 代码示例:**

```java
public static void main(String[] args) {
    Pattern p = Pattern.compile(""[^\\\\](?:\\\\\\\\)*\\\\{(\\\\d+)(?:,(\\\\d*))?\\\\}"");
    String testInput = ""[a-z0-9]{1,2}|[a-z0-9]{3,}|[a-z0-9]{4}"";
    Matcher matcher = p.matcher(testInput);
    while (matcher.find()){
        System.out.println(""下一个匹配项找到:"");
        if(matcher.group(2) == null){
            System.out.println(""严格:" + matcher.group(1));
        }else {
            System.out.println(""最小:""
                    + matcher.group(1)
                    + "", 最大:""
                    + (""".equals(matcher.group(2)) ? "无限" : matcher.group(2)));
        }
    }
}

输出:

下一个匹配项找到:
最小:1,最大:2
下一个匹配项找到:
最小:3,最大:无限
下一个匹配项找到:
严格:4

<details>
<summary>英文:</summary>

This regex [should work][2] for you: `[^\\](?:\\\\)*\{(\d+)(?:,(\d*))?\}`.

Part `[^\\](?:\\\\)*` addresses the case of slash escaping.

**Below is Java code example:**

```java
public static void main(String[] args) {
    Pattern p = Pattern.compile(&quot;[^\\\\](?:\\\\\\\\)*\\{(\\d+)(?:,(\\d*))?\\}&quot;);
    String testInput = &quot;[a-z0-9]{1,2}|[a-z0-9]{3,}|[a-z0-9]{4}&quot;;
    Matcher matcher = p.matcher(testInput);
    while (matcher.find()){
        System.out.println(&quot;Next matching found:&quot;);
        if(matcher.group(2) == null){
            System.out.println(&quot;Strict: &quot; + matcher.group(1));
        }else {
            System.out.println(&quot;Min: &quot;
                    + matcher.group(1)
                    + &quot;, Max: &quot;
                    + (&quot;&quot;.equals(matcher.group(2)) ? &quot;Infinte&quot; : matcher.group(2)));
        }
    }
}

Output:

Next matching found:
Min: 1, Max: 2
Next matching found:
Min: 3, Max: Infinte
Next matching found:
Strict: 4

huangapple
  • 本文由 发表于 2020年9月29日 23:12:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/64122506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定