Split data inside parenthesis to named groups using regex in golang

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

Split data inside parenthesis to named groups using regex in golang

问题

我有两种格式的行,我想使用正则表达式和命名组来分隔。

第一种格式是:

  1. a (>= 1.1)

第二种格式是:

  1. b (>= 1.1, < 2.0)

我想创建一些组,每个操作符都有一个匹配的版本,并且标记括号外的字母。

例如:

  1. n-> b
  2. o1 -> >=
  3. v1 -> 1.1
  4. o2 -> <
  5. v2 -> 2.0

我尝试了以下正则表达式:

  1. (?P<n>\S+) *(\(((?P<o>[>=<~]?[>=<~])? (?P<v>\S+))*\))?\s*$

但是我无法分隔括号内的文本。

请注意,在GO中,正则表达式不支持向前或向后查找。

有没有办法使用同一个正则表达式分隔内容?

谢谢!

英文:

I have to formats of lines that I want to separate using regex and named groups.
The 1st format is:

  1. a (&gt;= 1.1)

The 2nd format is:

  1. b (&gt;= 1.1, &lt; 2.0)

I want to create groups that will have for each operator a matching version, and also to mark the letter outside the parenthesis.

For example:

  1. n-&gt; b
  2. o1 -&gt; &gt;=
  3. v1 -&gt; 1.1
  4. o2 -&gt; &lt;
  5. v2 -&gt; 2.0

I've tried and created the below regex:

  1. (?P&lt;n&gt;\S+) *(\(((?P&lt;o&gt;[&gt;=&lt;~]?[&gt;=&lt;~])? (?P&lt;v&gt;\S+))*\))?\s*$

But I can't separate the text inside the parenthesis.

Please notice that in GO regex don't support look behind\ahead.

Is there a way to separate the content with the same regular expression?

Thanks!

答案1

得分: 0

@blackgreen 是正确的

  1. package main
  2. import (
  3. "fmt"
  4. "regexp"
  5. )
  6. func myRegx(s string) (n string, o []string, v []string) {
  7. regx := regexp.MustCompile(`(\S+) \(([>=<]+)\s+([\d\.]*)(,\s+([>=<]+)\s+([\d.]+))?\)`)
  8. b := regx.FindStringSubmatch(s)
  9. n = b[1]
  10. if len(b) < 4 {
  11. o = append(o, b[2])
  12. v = append(v, b[3])
  13. } else {
  14. o = append(o, b[2])
  15. v = append(v, b[3])
  16. o = append(o, b[5])
  17. v = append(v, b[6])
  18. }
  19. return n, o, v
  20. }
  21. func main() {
  22. n, o, v := myRegx("b (>= 1.1, < 2.0)")
  23. fmt.Printf("n: %v o:%v v:%v\n", n, o, v)
  24. n, o, v = myRegx("a (>= 1.1)")
  25. fmt.Printf("n: %v o:%v v:%v\n", n, o, v)
  26. }
英文:

@blackgreen is right

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;regexp&quot;
  5. )
  6. func myRegx(s string) (n string, o []string, v []string) {
  7. regx := regexp.MustCompile(`(\S+) \(([&gt;=&lt;]+)\s+([\d\.]*)(,\s+([&gt;=&lt;]+)\s+([\d.]+))?\)`)
  8. b := regx.FindStringSubmatch(s)
  9. n = b[1]
  10. if len(b) &lt; 4 {
  11. o = append(o, b[2])
  12. v = append(v, b[3])
  13. } else {
  14. o = append(o, b[2])
  15. v = append(v, b[3])
  16. o = append(o, b[5])
  17. v = append(v, b[6])
  18. }
  19. return n, o, v
  20. }
  21. func main() {
  22. n, o, v := myRegx(&quot;b (&gt;= 1.1, &lt; 2.0)&quot;)
  23. fmt.Printf(&quot;n: %v o:%v v:%v\n&quot;, n, o, v)
  24. n, o, v = myRegx(&quot;a (&gt;= 1.1)&quot;)
  25. fmt.Printf(&quot;n: %v o:%v v:%v\n&quot;, n, o, v)
  26. }

huangapple
  • 本文由 发表于 2022年10月30日 22:23:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/74253836.html
匿名

发表评论

匿名网友

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

确定