【问题标题】:a regular expression to grep specific paragraphs of a file用于 grep 文件特定段落的正则表达式
【发布时间】:2012-06-16 02:04:36
【问题描述】:

嗨,我正在编写一个 shellscript.. 假设这是我的 shell 脚本运行的数据

      Ownership
               o Australian Owned
   ?
   Ads for Mining Engineers
   232 results for
mining engineers in All States
   filtered by Mining Engineers [x] category
     * [ ]
                    [34]get directions
       Category:
       [35]Mining Engineers
       [36]Arrow Electrical Services in Wollongong, NSW under Mining
       Engineers logo
            [37]email
            [38]send to mobile
            [39]info
            Compare (0)
     * [ ]
       . [40]Firefly International
       Designers & Manufacturers. Service, Repair & Hire.
       We are the provider of mining engineers in Mt Thorley, NSW.
       25 Thrift Cl, Mt Thorley NSW 2330
       ph: (02) 6574 6660
            [41]http://www.fireflyint.com.au
            [42]get directions
       Category:
       [43]Mining Engineers
       [44]Firefly International in Mt Thorley, NSW under Mining Engineers
       logo
            [45]email
            [46]send to mobile
            [47]info
            Compare (0)
     * [ ]
       [48]Materials Solutions
       Materials Research & Development, Slurry Rheology & Piping Design.
       We are a well established company servicing the mining industry &
       associated manufacturing industries in all areas.
       Thornlie WA 6108
       ph: (08) 6468 4118
            [49]www.materialssolutions.com.au
       Category:
       [50]Mining Engineers
       [51]Materials Solutions in Thornlie, WA under Mining Engineers logo
            [52]email
            [53]send to mobile
            [54]info
            Compare (0)
     * [ ]
       . [55]ATC Williams Pty Ltd
       Our services are available from concept to completion of the works.
       Today, as the rebranded ATC Williams, we continue to expand our
       operations across Australia and in locations around the world.
       Unit 1, 21 Teddington Rd, Burswood WA 6100
       ph: (08) 9355 1383
            [56]www.atcwilliams.com.au
            [57]get directions
       Category:
       [58]Mining Engineers
       [59]ATC Williams Pty Ltd in Burswood, WA under Mining Engineers
       logo
            [60]email
            [61]send to mobile
            [62]info
            Compare (0)

我需要获取类似这样的地址

 * [ ]
       . [55]ATC Williams Pty Ltd
       Our services are available from concept to completion of the works.
       Today, as the rebranded ATC Williams, we continue to expand our
       operations across Australia and in locations around the world.
       Unit 1, 21 Teddington Rd, Burswood WA 6100
       ph: (08) 9355 1383
            [56]www.atcwilliams.com.au

那我该怎么办.. 我一直在研究像

这样的正则表达式

^*(.?[\w\W?\s?]*)+(.com.au)$

但这没有帮助..当我给输入文件提供我想要的地址匹配时,它匹配地址..但是当批量给出时,它没有帮助。 所以有人可以帮帮我吗..

【问题讨论】:

  • 如果我使用这么长的表达式,我的 grep 正在搜索元字符的特定字符.. 就像我的意思是 \s 作为任何空格,它会搜索字母 's' :(

标签: regex shell unix scripting grep


【解决方案1】:

我发现您的正则表达式存在一些问题

^*(.?[\w\W?\s?]*)+(.com.au)$
 ^ ^           ^ ^ ^   ^
 1 1           2 2 1   1
  1. 需要转义的特殊字符

  2. 匹配直到最后一个“.com.au”的所有内容的贪婪量词,在量词后添加一个?,使其不贪婪 ==> 尽可能少地匹配(意味着直到第一个“.com.au”,位于行尾)。

    ==>这是你的主要问题

  3. 你嵌套量词*)+,你不需要那个

  4. 在您的示例中,“*”和“.”之间有空格,因此要么匹配空格,要么完全删除点,它将与您的字符类匹配。

  5. 行首和“*”之间也有空格

所以,试试这个

    ^\s*\*([\w\W?\s?]*?)(\.com\.au)$

here on Regexr

【讨论】:

  • 不..那不工作..我最近的尝试是
    *(\.?[\w\W?\s?]*)+([\w\W\ s\d]*)?([\W\w]*\.\a\u)*$
    这实际上匹配所有文本,而我只希望它匹配地址部分!
  • 如果我使用这么长的表达式,我的 grep 正在搜索元字符的特定字符.. 就像我的意思是 \s 作为任何空格,它会搜索字母 's' :( @stema
【解决方案2】:

试试这个

^\s*\*\s*\[ \][^\*]+?[.]com[.]au$

解释

^        # Assert position at the beginning of a line (at beginning of the string or after a line break character)
\s       # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
   *        # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\*       # Match the character “*” literally
\s       # Match a single character that is a “whitespace character” (spaces, tabs, and line breaks)
   *        # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\[       # Match the character “[” literally
\        # Match the character “ ” literally
\]       # Match the character “]” literally
[^\*]    # Match any character that is NOT a * character
   +?       # Between one and unlimited times, as few times as possible, expanding as needed (lazy)
[.]      # Match the character “.”
com      # Match the characters “com” literally
[.]      # Match the character “.”
au       # Match the characters “au” literally
$        # Assert position at the end of a line (at the end of the string or before a line break character)

【讨论】:

  • 看起来不错!但是当我尝试使用带有该表达式的 grep $ grep '^\s**\s*[ ][^*]+?[.]com[.]au$' 文件时,它没有给我任何结果。但是当我在正则表达式测试器中使用它时..它肯定有效..所以你能告诉我那里有什么问题吗? @Cylian
  • @KiranVemuri:您忘记在命令$ grep '^\s**\s*[ ][^*]+?[.]com[.]au$ 中的^\s* 之后转义一个*。将其更改为 $ grep '^\s*\*\s*[ ][^*]+?[.]com[.]au$ 或 ````$ grep '^\s**\s*[ ][^*]+?\.com\.au$``。希望这行得通。
  • 它和我说的一样.. 当我在正则表达式测试器中尝试时,这个表达式工作得非常好,但是当我在我的 linux 终端中 grep 时.. 它没有给我任何结果:(
  • 我什至不能在 libreofile 的搜索规定中使用正则表达式j
  • 你能不能修改正则表达式来匹配下面的文本也code * [ ] . [55]ATC Williams Pty Ltd Our services are available from concept to completion of the works. Today, as the rebranded ATC Williams, we continue to expand our operations across Australia and in locations around the world. Unit 1, 21 Teddington Rd, Burswood WA 6100 ph: (08) 9355 1383
猜你喜欢
  • 1970-01-01
  • 2016-03-30
  • 1970-01-01
  • 2021-09-06
  • 2014-08-05
  • 2018-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多