简体   繁体   English

正则表达式pspad基于其他模式的包含替换行中的项目

[英]Regex pspad to replace item in line based on inclusion of other pattern

I need to match an item in a line that, upon finding this, will replace another item in the line. 我需要匹配一行中的一个项目,找到该项目后,它将替换该行中的另一个项目。 Ex: 例如:

Find all lines containing: code="ap" Replace: quant="*****" with quant="0" 查找包含以下内容的所有行: code="ap" quant="*****"quant="0"替换quant="*****" quant="0"

Note that the quant field I will need to replace could have multiple variables between the quotes. 请注意,我将需要替换的量子字段在引号之间可能有多个变量。

I tried from another thread: Need: replace "BBB" with "CCC" but only in lines that contain the word "AAA" 我从另一个线程尝试过:需要:用“ CCC”替换“ BBB”,但仅在包含单词“ AAA”的行中

Search: ((?=.*?AAA)[^\\r\\n]*)(BBB) 搜索: ((?=.*?AAA)[^\\r\\n]*)(BBB)

Replace: $1CCC 替换: $1CCC

However, I'm not sure if it will work with the quotes included in my find or how to enter the variable data in the initial quant replace. 但是,我不确定它是否适用于我的发现中包含的引号,或者如何在初始定量替换中输入变量数据。

Description 描述

This regex will do the following: 此正则表达式将执行以下操作:

  • validate the line contains code="ap" , if the line does not contain this string then no replacements will be made on the line 验证该行包含code="ap" ,如果该行不包含此字符串,则该行将不进行替换
  • find the first quantity="somevalue" string and replace with quantity="0" 找到第一个quantity="somevalue"字符串并替换为quantity="0"
  • allow quantity 's value to be any value, to include spaces and other special characters 允许quantity的值是任何值,包括空格和其他特殊字符
  • avoid difficult edge cases 避免困难的情况
  • allow the code and quantity key names to appear in any order on the line 允许codequantity键名称以任意顺序显示在行上
  • allow the values to be surrounded by single or double quotes or no quotes 允许值用单引号或双引号或不带引号引起来

The Regular Expression 正则表达式

For this regex I used the case insenstive and multiline flags 对于此正则表达式,我使用了区分大小写和多行标记

^(?=(?:[^=\r\n]|='[^']*'|="[^"]*"|=[^'"][^\s]*)*?code=(['"]?)ap\1)((?:[^=\r\n]|='[^']*'|="[^"]*"|=[^'"][^\s]*)*?quantity=)(?:"[^"]*"|'[^']*'|[^\s\n\r]*)(.*?)$

Replace with: 用。。。来代替:

$2"0"$3

正则表达式可视化

Note: to see the image better right click and select open in new window 注意:要更好地查看图像,请右键单击并选择在新窗口中打开

Examples 例子

Source Text 源文本

Note the difficult edge case in the last several lines. 注意最后几行中的困难情况。

code="ap" quantity="SomeValue" other="values"
code="Not ap" quantity="SomeValue"
code="ap" quantity="SomeValue"
quantity="AlsoSomeValue2" code="ap" 
code="ap" other=' quantity="Save this value" ' quantity="SomeValue"
code="Not ap" quantity="SomeValue" other=' Code="ap" '

After Replace 更换后

code="ap" quantity="0" other="values"
code="Not ap" quantity="SomeValue"
code="ap" quantity="0"
quantity="0" code="ap" 
code="ap" other=' quantity="Save this value" ' quantity="0"
code="Not ap" quantity="SomeValue" other=' Code="ap" '

Explanation 说明

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  (?=                      look ahead to see if there is:
----------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the least amount
                             possible)):
----------------------------------------------------------------------
      [^=\r\n]                 any character except: '=', '\r'
                               (carriage return), '\n' (newline)
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      ='                       '=\''
----------------------------------------------------------------------
      [^']*                    any character except: ''' (0 or more
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
      '                        '\''
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      ="                       '="'
----------------------------------------------------------------------
      [^"]*                    any character except: '"' (0 or more
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
      "                        '"'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      =                        '='
----------------------------------------------------------------------
      [^'"]                    any character except: ''', '"'
----------------------------------------------------------------------
      [^\s]*                   any character except: whitespace (\n,
                               \r, \t, \f, and " ") (0 or more times
                               (matching the most amount possible))
----------------------------------------------------------------------
    )*?                      end of grouping
----------------------------------------------------------------------
    code=                    'code='
----------------------------------------------------------------------
    (                        group and capture to \1:
----------------------------------------------------------------------
      ['"]?                    any character of: ''', '"' (optional
                               (matching the most amount possible))
----------------------------------------------------------------------
    )                        end of \1
----------------------------------------------------------------------
    ap                       'ap'
----------------------------------------------------------------------
    \1                       what was matched by capture \1
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the least amount
                             possible)):
----------------------------------------------------------------------
      [^=\r\n]                 any character except: '=', '\r'
                               (carriage return), '\n' (newline)
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      ='                       '=\''
----------------------------------------------------------------------
      [^']*                    any character except: ''' (0 or more
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
      '                        '\''
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      ="                       '="'
----------------------------------------------------------------------
      [^"]*                    any character except: '"' (0 or more
                               times (matching the most amount
                               possible))
----------------------------------------------------------------------
      "                        '"'
----------------------------------------------------------------------
     |                        OR
----------------------------------------------------------------------
      =                        '='
----------------------------------------------------------------------
      [^'"]                    any character except: ''', '"'
----------------------------------------------------------------------
      [^\s]*                   any character except: whitespace (\n,
                               \r, \t, \f, and " ") (0 or more times
                               (matching the most amount possible))
----------------------------------------------------------------------
    )*?                      end of grouping
----------------------------------------------------------------------
    quantity=                'quantity='
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    "                        '"'
----------------------------------------------------------------------
    [^"]*                    any character except: '"' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    "                        '"'
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    '                        '\''
----------------------------------------------------------------------
    [^']*                    any character except: ''' (0 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    '                        '\''
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    [^\s\n\r]*               any character except: whitespace (\n,
                             \r, \t, \f, and " "), '\n' (newline),
                             '\r' (carriage return) (0 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  (                        group and capture to \3:
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
  )                        end of \3
----------------------------------------------------------------------
  $                        before an optional \n, and the end of a
                           "line"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM