繁体   English   中英

Reg ex查找并替换为组

[英]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 $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 ++中使用reg ex来完成此任务。 到目前为止,这是我想出的。 screen_name:\\ s [A-Za-z0-9] +这就是我遇到的问题,因为我不确定如何替换第一个和最后一个字符。

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

采用

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

(screen_name:\\s\\w)捕获screen_name:并在替换模式中将其称为第1组的空白,后来称为$1\\w*仅匹配0+个单词字符,然后(\\w)匹配并捕获单个单词char进入第2组,后来从替换模式中称为$2

替换为$1****$2

参见regex演示

在此处输入图片说明

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

暂无
暂无

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

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