其他教程

其他教程

Products

当前位置:首页 > 其他教程 >

正则表达式首次出现括号

GG网络技术分享 2025-03-18 16:15 19


问题描述:

I have a string like this:

Adress: Big Avenue 9 - National (FOREST (THE)).

And i need parse it with regex to retrieve some data like this:

  • Big Avenue 9 - National
  • FOREST (THE)

I\'m using this regex:

/Adress\\:\\s(.*(?!\\.*\\)))\\((.*[\\(.*\\)]?)\\)/

But they giveme this result:

  • Big Avenue 9 - National (FOREST
  • THE)

I\'ve searching lot\'s information, but I don\'t know how can I do it.

Thank you so much for your help.

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我有一个这样的字符串:</ p>

地址 :Big Avenue 9 - National(FOREST(THE))。</ p>

</ blockquote>

我需要用正则表达式解析它以检索这样的数据:</ p>

  • Big Avenue 9 - National </ li>

  • FOREST(THE)</ li>

    </ ul>

    我正在使用此 正则表达式:</ p>

     <代码> / ADRESS \\:\\ S(?!*(\\ * \\)))\\(?(* [\\(* \\)])  \\)/ 

    </ code> </ pre>

    但他们给出了这个结果:</ p>

    • Big Avenue 9 - National(FOREST < / li>

    • THE)</ li>

      </ ul>

      我正在搜索很多信息,但我不知道怎么办。

      谢谢你 非常适合你的帮助。</ p>

      </ div>

      网友观点:

      You may use

      \'~Adress:\\s*(.*?)\\s*\\((.*)\\)~\'

      See the PHP regex demo, results

      Details

      • Adress: - a literal substring
      • \\s* - 0+ whitespaces
      • (.*?) - Group 1: any 0+ chars other than line break chars, as few as possible
      • \\s* - 0+ whitespaces
      • \\( - a ( char
      • (.*) - Group 2: any 0+ chars other than line break chars, as many as possible
      • \\) - a ) char

      See a PHP demo:

      $re = \'/Adress:\\s*(.*?)\\s*\\((.*)\\)/\';

      $str = \'Adress: Big Avenue 9 - National (FOREST (THE)).\';

      if (preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0)) {

      print_r($matches);

      }

      You can match anything up to the first ( by ([^(]*) and then anything between the optional ( ... ) with (\\(.*\\))?

      Adress\\:\\s([^(]*)(\\(.*\\))?

      Play here: https://regexr.com/3kp5i

      求大佬解释一下下图中的正则表达式?

      re.findall()后加括号
      https://www.crifan.com/python_re_search_vs_re_findall/

      解答的很详细

      再次补充:
      1、python分组不捕获(?:...)
      2、python分组命名捕获(?p<name>...)
      3、python分组不命名捕获(...)

      第1个是整体正则表达式为元素构成的列表
      第2、3个一般是分组匹配的元祖构成的列表(注:每个元祖内元素的个数等于左括号的个数)

      匹配的主要规则:
      是从左到右看左括号出现的先后顺序作为分组匹配先后顺序的依据

      ((o\\d){1})

      组一为 ((o\\d){1})
      组二为 (o\\d)

      组一第一次匹配得到 o1
      组二的父括号为组一,所以以组一得到的匹配进行匹配得到o1

      组一第二次匹配得到 o2
      组二的父括号为组一,所以以组一得到的匹配进行匹配得到o2

      组一第3次匹配得到 o3
      组二的父括号为组一,所以以组一得到的匹配进行匹配得到o3

      所以得到:
      [(“O1”,“O1”),(“O2”,“O2”),(“O3”,“O3”)]


      其他都好理解,目前对于类似:

      import re

      re.findall(r"(a\\d){2}","a1a2a3a4a5a6")

      #['a2', 'a4', 'a6']

      理解起来还有点晕。


      不会写/分析调试正则,都是没遇到好的工具而已,推荐regexbuddy,右侧的listall功能简直不要太全。

标签:

提交需求或反馈

Demand feedback