Reg ex查找并替换为组

时间:2018-03-10 07:09:08

标签: regex notepad++ regex-group

我试图在每行文字中找到屏幕名称,例如。 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 $8745.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 $8745.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 ++中数据的样子: enter image description here

我在Notepad ++中使用reg ex执行此任务。这是我到目前为止所提出的。 screen_name:\ s [A-Za-z0-9] +然后这就是我卡住的地方,因为我不知道如何替换第一个和最后一个字符。

2 个答案:

答案 0 :(得分:1)

您可以在替换模式中使用正则表达式模式中的捕获组和替换后引用(也称为占位符)。此外,如果您想匹配字母,数字和下划线,请使用\w代替自定义[a-zA-Z0-9]

使用

(screen_name:\s\w)\w*(\w)

(screen_name:\s\w)从替换模式中捕获screen_name:以及后面称为$1的第1组中的空格,\w*只匹配0+个字符,然后{ {1}}从替换模式匹配并捕获单个字符char到组2中,稍后称为(\w)

替换为$2

请参阅regex demo

enter image description here

答案 1 :(得分:-1)

(screen_name:\s[A-Za-z0-9_])[A-Za-z0-9_]*([A-Za-z0-9_]) => $1****$2