匹配 1-3:6-8:13-15 等的正则表达式

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

A regular expression that matches 1-3:6-8:13-15 etc

问题

我有一个字符串模式,可以是:

  1. 一个数字(例如1)
  2. 一个数字范围(例如1-2)
  3. 一组数字(例如1:4:7)
  4. 一组范围(例如1-2:5-7:12-15)

所以,类似"1"、"1-4:4"、"6:9-12:20"的字符串都是有效的。

我已经在RegExr.com上创建了一个模式:

/(\d(-\d)?)(:(\d(-\d)?))?/g

这个模式几乎可以工作,但还不够,因为它无法匹配超过第一对的内容。例如:

1-2:7-9:12-23
       ^

箭头所示的字符由于某种原因没有匹配上。

有人可以提供建议吗?

英文:

I have a string pattern that can be

  1. a number (ex 1)
  2. a number range (ex 1-2)
  3. a list of numbers (ex 1:4:7)
  4. a list of ranges (ex 1-2:5-7:12-15)

So, string like "1", "1-4:4", "6:9-12:20" are all valid

I have created a pattern on RegExr.com so far

/(\d(-\d)?)(:(\d(-\d)?))?/g

which is almost working, but not quite, as it can't go past the first pair. For example:

1-2:7-9:12-23
       ^

the character denoted with the arrow is not matched for some reason.

Can someone please advise?

答案1

得分: 1

你可以尝试这个模式:/^(\d+(-\d+)?)(:(\d+(-\d+)?))*$/gm

这是你模式的修改版本,添加了起始/结束字符串锚点,将你最后一组的 ? 量词替换为 *(零次或多次),并使用 + 量词来匹配数字组。

这里查看正则表达式演示。

英文:

You could try this pattern: /^(\d+(-\d+)?)(:(\d+(-\d+)?))*$/gm

This is modified version of your pattern, which added start/end string anchors, replaced ? quantifier of your last group to * (zero or more) quantifier, and + quantifier for matching group of digit numbers.

See regex demo here.

答案2

得分: 0

以下是代码部分的翻译:

public static void main(String[] args) {
    String pattern = "(?:(?:^|:)(\\d+)(?:-(\\d+))?)+";
    Stream.of(
        "1",
        "1-2.",
        "1:4:7",
        "1-2:5-7:12-15",
        "1-4:4",
        "6:9-12:20",

        "123",
        "123-234",
        "123:456:789",
        "123-234:567-789:1234-1567",
        "123-456:456",
        "678:999-1234:2012"
    ).forEach(it -> {
        System.out.println(it + " => " + it.matches(pattern));
    });
}

Stdout:

1 => true
1-2. => false
1:4:7 => true
1-2:5-7:12-15 => true
1-4:4 => true
6:9-12:20 => true
123 => true
123-234 => true
123:456:789 => true
123-234:567-789:1234-1567 => true
123-456:456 => true
678:999-1234:2012 => true
英文:

Each group could start from the beginning of the string or with ":": /^|:/. This way regex could be simplified into a single group definition:

public static void main(String[] args) {
    String pattern = "(?:(?:^|:)(\\d+)(?:-(\\d+))?)+";
    Stream.of(
        "1",
        "1-2.",
        "1:4:7",
        "1-2:5-7:12-15",
        "1-4:4",
        "6:9-12:20",

        "123",
        "123-234",
        "123:456:789",
        "123-234:567-789:1234-1567",
        "123-456:456",
        "678:999-1234:2012"
    ).forEach(it -> {
        System.out.println(it + " => " + it.matches(pattern));
    });
}

Stdout:

1 => true
1-2. => false
1:4:7 => true
1-2:5-7:12-15 => true
1-4:4 => true
6:9-12:20 => true
123 => true
123-234 => true
123:456:789 => true
123-234:567-789:1234-1567 => true
123-456:456 => true
678:999-1234:2012 => true

huangapple
  • 本文由 发表于 2023年3月9日 19:17:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683860.html
匿名

发表评论

匿名网友

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

确定