regex 我如何让我的正则表达式排除在我的负前瞻中指示的字符串?

mcdcgff0  于 2023-06-30  发布在  其他
关注(0)|答案(2)|浏览(111)

我的正则表达式匹配任何既包含数字又包含字母的字符串,包括罗马数字。谁能帮我弄清楚如何排除我的负lookahead中指示的字符串?我试过切换到负向后查找并添加一个\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

lskq00tm

lskq00tm1#

使用

(?:[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
eoigrqb6

eoigrqb62#

谢谢你的帮助!在浏览了你所有的建议并尝试了不同的东西之后,这个最终的正则表达式最终完成了我需要它做的一些细节:

(?:[0-90-9⁰-⁹₀-₉]+[μⅠⅡⅢⅣⅥⅦⅧⅨⅪⅫⅤⅩⅬⅭⅮⅯa-za-zA-ZA-Z]+|[μⅠⅡⅢⅣⅥⅦⅧⅨⅪⅫⅤⅩⅬⅭⅮⅯa-za-zA-ZA-Z]+[0-90-9⁰-⁹₀-₉]+)[μⅠⅡⅢⅣⅥⅦⅧⅨⅪⅫⅤⅩⅬⅭⅮⅯa-za-zA-ZA-Z0-90-9⁰-⁹₀-₉]*|
[0-90-9⁰-⁹₀-₉]+(?! grams)(?: mm| cm| nm| M| ppm| g | mg| kg| μL| μm| μmol| μg| μM| ng| mol| mmol| mM| nM| mL| v\/v| U| rpm|重量ppm|重量%ppm|モル| W| kcal| GHz| pF| fF| dB| V| m2)

相关问题