根据前一行是否存在特定字符串来修改文本。

huangapple go评论58阅读模式
英文:

Looking to modify text depending on a certain character string being present on the previous line

问题

我理解你想要使用正则表达式和Notepad++来修改文本格式。你的目标是在每个IP地址后面插入文本字符串“ABC123”。以下是你的描述的步骤:

  1. 在每个IP地址后插入字符串“ABC123”。

你可以使用以下正则表达式和替换模式来实现:

查找:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+([\s\S]*?)(?=\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|\z)

替换为:\1\nABC123\n\2

请确保在Notepad++中选择了“正则表达式”选项。

使用这个正则表达式和替换模式,它将在每个IP地址后插入“ABC123”。

希望对你有所帮助!

英文:

I have text of the below format that I wish to modify using regex and notepad++.

Format of text:

16.232.39.195 
dwdwevevveeve
148.92.235.232
49.58.203.107
130.221.168.79 
vfeevvewdwdqwdq
2.170.109.98
254.250.64.253
10.102.107.236
155.146.118.222 
ntyrovgmnfewijw
47.80.127.125 
ewfwfmbbrmbve
26.232.99.92
10.0.46.127
229.154.77.234
35.15.165.153 
fewomwvmvvm
157.27.74.183
78.244.169.225
114.7.107.67 
xfevwwf
13.118.248.99 
wefwfwwf
116.102.16.22 
wfgheegfwf
22.4.118.222
61.205.56.191

Explanation of how I want to modify the above data format using regex and notepadd++ :

The above data format is a list of IP addresses. You will note that on the line after some of the IP addresses, there is a text string, and after some other IP addresses, there is no text string. For the IP addresses that currently don't have a text string in the following line, I want to insert a text string. Let's say for illustrative purposes I want to insert the text string 'ABC123' in the line after each IP address that currently isn't followed by a text string. If I could do this successfully, my data would be modified to look like the below:

16.232.39.195
dwdwevevveeve
148.92.235.232
ABC123
49.58.203.107
ABC123
130.221.168.79
vfeevvewdwdqwdq
2.170.109.98
ABC123
254.250.64.253
ABC123
10.102.107.236
ABC123
155.146.118.222
ntyrovgmnfewijw
47.80.127.125
ewfwfmbbrmbve
26.232.99.92
ABC123
10.0.46.127
ABC123
229.154.77.234
ABC123
35.15.165.153
fewomwvmvvm
157.27.74.183
ABC123
78.244.169.225
ABC123
114.7.107.67
xfevwwf
13.118.248.99
wefwfwwf
116.102.16.22
wfgheegfwf
22.4.118.222
ABC123
61.205.56.191
ABC123

So in algorithmic terms I want to do something like:

for IP address on line A:

if string on line B = IP address
then insert a new line after line A that contains the string 'ABC123'
 
elseif string on line B = character string
then do nothing 
 
repeat the above process for the next IP address in the document. 

I know that if all of the text were on a single line, I would be able to solve the problem with the below regex / notepad++ method:

Find: \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})
 
replace with:  ABC123 

But it is the fact that each IP address and text string is on a separate line that I am not sure how to solve.

Any advice on the best way to solve this would be greatly appreciated.

答案1

得分: 2

  • <kbd>Ctrl</kbd>+<kbd>H</kbd>
  • 查找内容: (\d{1,3}(?:\.\d{1,3}){3})\K(?=\R(?1)|\z)
  • 替换为: \nABC123
  • TICK 环绕
  • SELECT 正则表达式
  • <kbd>全部替换</kbd>
英文:
  • <kbd>Ctrl</kbd>+<kbd>H</kbd>
  • Find what: (\d{1,3}(?:\.\d{1,3}){3})\K(?=\R(?1)|\z)
  • Replace with: \nABC123
  • TICK Wrap around
  • SELECT Regular expression
  • <kbd>Replace all</kbd>

Explanation:

(               # group 1
    \d{1,3}         # 1 upto 3 digits
    (?:             # non capture group
        \.              # a dot
        \d{1,3}         # 1 upto 3 digits
    ){3}            # end group, must appear 3 times
)               # end group 1
\K              # forget all we have seen until this position
(?=             # positive lookahead, make sure we have after:
    \R              # any kind of linebreak
    (?1)            # same pattern as used in group 1 (IP address)
  |               # OR
    \z              # end of file
)               # end lookahead

Replacement:

\n              # linefeed, you can use \r\n for Windows EOL
ABC123

Screenshot (before):

根据前一行是否存在特定字符串来修改文本。

Screenshot (after):

根据前一行是否存在特定字符串来修改文本。

答案2

得分: 0

在你的示例数据中,IP地址后跟文本时,IP地址末尾始终带有空格。请注意,这不是有效的IP地址。

如果这在你问题的初始数据中是正确的,你可以执行以下步骤:

  • Ctrl+H
  • 查找内容:(^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$)
  • 替换为:\1\r\nABC123
  • 搜索模式:正则表达式
  • 单击全部替换
英文:

In your sample data, IP addresses always have a space at the end when followed by text. Please note this is not a valid IP address.

In case this is correct in the initial data of your question, you can apply the following steps:

  • <kbd>Ctrl</kbd>+<kbd>H</kbd>
  • Find what: (^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$)
  • Replace with: \1\r\nABC123
  • Search mode: Regular expression
  • Click on <kbd>Replace All</kbd>

huangapple
  • 本文由 发表于 2023年2月16日 19:21:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定