【问题标题】:How do I get my regex to exclude the strings indicated in my negative lookahead?如何让我的正则表达式排除负前瞻中指示的字符串?
【发布时间】:2021-07-04 05:06:43
【问题描述】:

我的正则表达式匹配任何包含数字和字母的字符串,包括罗马数字。谁能帮我弄清楚如何排除我的否定前瞻中指示的字符串?我尝试切换到负后视并添加 \b 边界,这解决了这个问题,除了它不会包含全角字符,因为 \b 不包含它们。我很茫然。

(?:[0-90-9]+[ⅠⅡⅢⅣⅥⅦⅧⅨⅪⅫⅤⅩⅬⅭⅮⅯA-zA-z]+|[ⅠⅡⅢⅣⅥⅦⅧⅨⅪⅫⅤⅩⅬⅭⅮⅯA-zA-z]+[0-90-9]+)[ⅠⅡⅢⅣⅥⅦⅧⅨⅪⅫⅤⅩⅬⅭⅮⅯA-zA-z0-90-9]*(?!mg/kg|mg\/kg|nm|nm|MHz|ppm|ppm|mmol|mmol|g|g|g|mL|mL|mol|mol|nM|nM|μL|v\/v)

https://regex101.com/r/u82LRb/12

【问题讨论】:

  • 喜欢这个? regex101.com/r/jqH77t/1
  • 我认为你可以使用this regex。请通过评论提供反馈,让我们知道它是否有效,如果无效,您的期望是什么。

标签: regex


【解决方案1】:

使用

(?:[0-90-9]+[ⅠⅡⅢⅣⅥⅦⅧⅨⅪⅫⅤⅩⅬⅭⅮⅯA-Za-zA-z]+|[ⅠⅡⅢⅣⅥⅦⅧⅨⅪⅫⅤⅩⅬⅭⅮⅯA-Za-zA-z]+[0-90-9]+)[ⅠⅡⅢⅣⅥⅦⅧⅨⅪⅫⅤⅩⅬⅭⅮⅯA-Za-zA-z0-90-9]*(?!\p{L})(?<!mg(?=/kg)|mg(?=\/kg)|nm|nm|MHz|ppm|ppm|mmol|mmol|mL|mL|mol|mol|nM|nM|μL|v(?=\/v)|[gg])

regex proof

解释

--------------------------------------------------------------------------------
  (?:                      group, but do not capture:
--------------------------------------------------------------------------------
    [0-90-9]+           any character of: '0' to '9' (1 or more times 
                           (matching the most  amount possible))
--------------------------------------------------------------------------------
    [ⅠⅡⅢⅣⅥⅦⅧⅨⅪⅫⅤⅩⅬⅭⅮⅯ       any Roman number or 'A' to 'Z', 'a' to 'z'
     A-Za-zA-z]+                   or 'A' to 'z'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    [ⅠⅡⅢⅣⅥⅦⅧⅨⅪⅫⅤⅩⅬⅭⅮⅯ       any Roman number or 'A' to 'Z', 'a' to 'z'
     A-Za-zA-z]+                   or 'A' to 'z'
--------------------------------------------------------------------------------
    [0-90-9]+              any character of: '0' to '9'
                             (1 or more times (matching the most amount possible))
--------------------------------------------------------------------------------
  )                        end of grouping
--------------------------------------------------------------------------------
  [ⅠⅡⅢⅣⅥⅦⅧⅨⅪⅫⅤ       any Roman number, or 'A' to 'Z', 'a' to 'z'
  ⅩⅬⅭⅮⅯA-Za-z             or 'A' to 'z'
  A-z0-90-9]*          
--------------------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
--------------------------------------------------------------------------------
    \p{L}                    any Unicode letter character 
--------------------------------------------------------------------------------
  )                        end of look-ahead
--------------------------------------------------------------------------------
  (?<!                     look behind to see if there is not:
--------------------------------------------------------------------------------
    mg                    'mg'
--------------------------------------------------------------------------------
    (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
      /kg                      '/kg'
--------------------------------------------------------------------------------
    )                        end of look-ahead
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    mg                       'mg'
--------------------------------------------------------------------------------
    (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
      \/kg                       '/kg'
--------------------------------------------------------------------------------
    )                        end of look-ahead
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
   nm                     'nm'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    nm                       'nm'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    MHz                      'MHz'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    ppm                  'ppm'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    ppm                      'ppm'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    mmol                     'mmol'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    mmol                'mmol'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    mL                       'mL'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    mL                  'mL'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    mol                      'mol'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    mol                  'mol'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    nM                       'nM'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
   nM                      'nM'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    μL                  'μL'
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    v                        'v'
--------------------------------------------------------------------------------
    (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
      \/v                        '/v'
--------------------------------------------------------------------------------
    )                        end of look-ahead
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    [gg]                   any character of: 'g' or 'g'
--------------------------------------------------------------------------------
  )                        end of look-behind

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2011-10-14
    • 2010-12-17
    • 1970-01-01
    相关资源
    最近更新 更多