正则表达式查找并替换为组
Reg ex find and replace with groups
我正在尝试在每行文本中查找屏幕名称,例如。 screen_name:CoinLibre2009。找到每个屏幕名称后,我必须用原来的第一个字符替换每个屏幕名称,然后正好是四个星号 (****),然后是最后一个字符。例如,屏幕名称 CoinLibre2009 将变为 C****9。我在想我需要使用组,我应该在我的查找中包含 "screen_name: ",然后将它包含在替换中。
以下是我正在处理的几行文本:
posted: Sat Feb 03 2018 11:03:09 text: Today we can see positive trends for growth, but will there be a new fall? crypto screen_name: Ksandimo location: null verified: false followers_count: 1597 friends_count: 17 lang: ru retweet_count: 0 favorite_count: 0
posted: Sat Feb 03 2018 11:03:14 text: 8745.02$ per now screen_name: CoinLibre2009 location: Free World verified: false followers_count: 113 friends_count: 110 lang: ru retweet_count: 0 favorite_count: 0
posted: Sat Feb 03 2018 11:03:16 text: Current price of is 45.02 screen_name: bitcoinavg location: null verified: false followers_count: 44 friends_count: 9 lang: en retweet_count: 0 favorite_count: 0
posted: Sat Feb 03 2018 11:03:25 text: Think weve hit resistance for Bitcoin now. Will it fully recover? Im not sure screen_name: jasongaved location: Brighton & Hove / London verified: false followers_count: 1996 friends_count: 1967 lang: en retweet_count: 0 favorite_count: 0
posted: Sat Feb 03 2018 11:03:28 text: Today's price is 45.02 as of February 3, 2018 at 11:59AM screen_name: FR33Q location: Europe verified: false followers_count: 1164 friends_count: 1998 lang: en retweet_count: 0 favorite_count: 0
这里还有一个截图,显示了记事本++中的数据:
我在 Notepad++ 中使用 reg ex 来完成这项任务。到目前为止,这是我想出的。 screen_name:\s[A-Za-z0-9]+ 这就是我卡住的地方,因为我不确定如何替换第一个和最后一个字符。
(screen_name:\s[A-Za-z0-9_])[A-Za-z0-9_]*([A-Za-z0-9_]) => ****
您可以在正则表达式模式中使用捕获组,在替换模式中使用替换反向引用(也称为占位符)。此外,如果要匹配字母、数字 和下划线 ,请使用 \w
而不是自定义 [a-zA-Z0-9]
.
使用
(screen_name:\s\w)\w*(\w)
(screen_name:\s\w)
从替换模式中捕获 screen_name:
和一个空格到第 1 组,后来称为 </code>,<code>\w*
只匹配 0+ 个字符和然后 (\w)
匹配并将单个单词 char 捕获到组 2 中,后来从替换模式中称为 </code>。</p>
<p>替换为<code>****
。
参见regex demo。
我正在尝试在每行文本中查找屏幕名称,例如。 screen_name:CoinLibre2009。找到每个屏幕名称后,我必须用原来的第一个字符替换每个屏幕名称,然后正好是四个星号 (****),然后是最后一个字符。例如,屏幕名称 CoinLibre2009 将变为 C****9。我在想我需要使用组,我应该在我的查找中包含 "screen_name: ",然后将它包含在替换中。
以下是我正在处理的几行文本:
posted: Sat Feb 03 2018 11:03:09 text: Today we can see positive trends for growth, but will there be a new fall? crypto screen_name: Ksandimo location: null verified: false followers_count: 1597 friends_count: 17 lang: ru retweet_count: 0 favorite_count: 0
posted: Sat Feb 03 2018 11:03:14 text: 8745.02$ per now screen_name: CoinLibre2009 location: Free World verified: false followers_count: 113 friends_count: 110 lang: ru retweet_count: 0 favorite_count: 0
posted: Sat Feb 03 2018 11:03:16 text: Current price of is 45.02 screen_name: bitcoinavg location: null verified: false followers_count: 44 friends_count: 9 lang: en retweet_count: 0 favorite_count: 0
posted: Sat Feb 03 2018 11:03:25 text: Think weve hit resistance for Bitcoin now. Will it fully recover? Im not sure screen_name: jasongaved location: Brighton & Hove / London verified: false followers_count: 1996 friends_count: 1967 lang: en retweet_count: 0 favorite_count: 0
posted: Sat Feb 03 2018 11:03:28 text: Today's price is 45.02 as of February 3, 2018 at 11:59AM screen_name: FR33Q location: Europe verified: false followers_count: 1164 friends_count: 1998 lang: en retweet_count: 0 favorite_count: 0
这里还有一个截图,显示了记事本++中的数据:
我在 Notepad++ 中使用 reg ex 来完成这项任务。到目前为止,这是我想出的。 screen_name:\s[A-Za-z0-9]+ 这就是我卡住的地方,因为我不确定如何替换第一个和最后一个字符。
(screen_name:\s[A-Za-z0-9_])[A-Za-z0-9_]*([A-Za-z0-9_]) => ****
您可以在正则表达式模式中使用捕获组,在替换模式中使用替换反向引用(也称为占位符)。此外,如果要匹配字母、数字 和下划线 ,请使用 \w
而不是自定义 [a-zA-Z0-9]
.
使用
(screen_name:\s\w)\w*(\w)
(screen_name:\s\w)
从替换模式中捕获 screen_name:
和一个空格到第 1 组,后来称为 </code>,<code>\w*
只匹配 0+ 个字符和然后 (\w)
匹配并将单个单词 char 捕获到组 2 中,后来从替换模式中称为 </code>。</p>
<p>替换为<code>****
。
参见regex demo。