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

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

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. 1-2:7-9:12-23
  2. ^

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

有人可以提供建议吗?

英文:

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. 1-2:7-9:12-23
  2. ^

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

以下是代码部分的翻译:

  1. public static void main(String[] args) {
  2. String pattern = "(?:(?:^|:)(\\d+)(?:-(\\d+))?)+";
  3. Stream.of(
  4. "1",
  5. "1-2.",
  6. "1:4:7",
  7. "1-2:5-7:12-15",
  8. "1-4:4",
  9. "6:9-12:20",
  10. "123",
  11. "123-234",
  12. "123:456:789",
  13. "123-234:567-789:1234-1567",
  14. "123-456:456",
  15. "678:999-1234:2012"
  16. ).forEach(it -> {
  17. System.out.println(it + " => " + it.matches(pattern));
  18. });
  19. }

Stdout:

  1. 1 => true
  2. 1-2. => false
  3. 1:4:7 => true
  4. 1-2:5-7:12-15 => true
  5. 1-4:4 => true
  6. 6:9-12:20 => true
  7. 123 => true
  8. 123-234 => true
  9. 123:456:789 => true
  10. 123-234:567-789:1234-1567 => true
  11. 123-456:456 => true
  12. 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:

  1. public static void main(String[] args) {
  2. String pattern = "(?:(?:^|:)(\\d+)(?:-(\\d+))?)+";
  3. Stream.of(
  4. "1",
  5. "1-2.",
  6. "1:4:7",
  7. "1-2:5-7:12-15",
  8. "1-4:4",
  9. "6:9-12:20",
  10. "123",
  11. "123-234",
  12. "123:456:789",
  13. "123-234:567-789:1234-1567",
  14. "123-456:456",
  15. "678:999-1234:2012"
  16. ).forEach(it -> {
  17. System.out.println(it + " => " + it.matches(pattern));
  18. });
  19. }

Stdout:

  1. 1 => true
  2. 1-2. => false
  3. 1:4:7 => true
  4. 1-2:5-7:12-15 => true
  5. 1-4:4 => true
  6. 6:9-12:20 => true
  7. 123 => true
  8. 123-234 => true
  9. 123:456:789 => true
  10. 123-234:567-789:1234-1567 => true
  11. 123-456:456 => true
  12. 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:

确定