用于否定三个@后跟数字和最后三个@的正则表达式
Regex for negation of three @ followed by number and three @ at end
我需要构建一个正则表达式,它在开始时以速率符号 @
取反三个,然后是 1 到 12 位数字之间的不同长度的数字,并以三个 @
结尾象征。应选择除此之外的任何内容。
基本上我的挑战是我有一个数据框,它有一个文本语料库和一个模式值 @@@0-9@@@
我想删除除了这个模式之外的所有东西。我已经能够将正则表达式开发为 [@][@][@]\d{1,12}[@][@][@]
但是我想否定这种模式,因为我想查找和替换。例如
my name is x and i work at @@@12354@@@ and i am happy with my job. what is your company name? is it @@@42334@@@? you look happy as well!!
应该 return @@@12354@@@ @@@42334@@@
在单个元素之间有一个 space 分隔符会很棒。有帮助吗?
我将在 python pandas 数据帧 uisng str.replace
函数中使用此正则表达式。
我已经尝试了 regexr.com and regex101.com 并且到目前为止
**编辑:**下面是数据
SNo details
1 account @@@0000082569@@@ / department stores uk & ie credit control operations
2 academic @@@0000060910@@@ , administrative, and @@@0000039198@@@ liaison coordinator
3 account executive, financial @@@0000060910@@@ , enterprise and partner group
4 2015-nasa summer internship- space power system @@@0000129849@@@ and testing
5 account technical @@@0000185187@@@ , technical presales, systems engineer
6 account @@@0000082569@@@ for car, van & 4x4 products in the east of england
7 account @@@0000082569@@@ for mikro segment and owners of the enterprises
8 account @@@0000082569@@@ - affinity digital display, mobile & publishing
9 account @@@0000082569@@@ @@@0000060905@@@ -energy and commodities @@@0000086889@@@ candidate
10 account @@@0000082569@@@ for companies department of external relevance
而不是 replace
和复杂的正则表达式,你可以使用 join
和 findall
并使用更简单的正则表达式,如下所示:
>>> str = 'my name is x and i work at @@@12354@@@ and i am happy with my job. what is your company name? is it @@@42334@@@? you look happy as well!!'
>>> ' '.join(re.findall(r'@{3}\d{1,12}@{3}', str))
'@@@12354@@@ @@@42334@@@'
这就是我在 中的意思:
>>> df = pd.DataFrame({'col1':['at @@@12354@@@ and i am happy with my job. what is your company name? is it @@@42334@@@? you look happy as well!!', 'at @@@222@@@ and t @@@888888@@@?' ]})
>>> df['col1'].str.findall(r'@{3}\d+@{3}').apply(' '.join)
0 @@@12354@@@ @@@42334@@@
1 @@@222@@@ @@@888888@@@
@{3}\d+@{3}
将匹配任何包含 3 个 @
符号的 1+ 数字,.findall
将提取所有匹配项。 .apply(' '.join)
将用 space.
加入值
我需要构建一个正则表达式,它在开始时以速率符号 @
取反三个,然后是 1 到 12 位数字之间的不同长度的数字,并以三个 @
结尾象征。应选择除此之外的任何内容。
基本上我的挑战是我有一个数据框,它有一个文本语料库和一个模式值 @@@0-9@@@
我想删除除了这个模式之外的所有东西。我已经能够将正则表达式开发为 [@][@][@]\d{1,12}[@][@][@]
但是我想否定这种模式,因为我想查找和替换。例如
my name is x and i work at @@@12354@@@ and i am happy with my job. what is your company name? is it @@@42334@@@? you look happy as well!!
应该 return @@@12354@@@ @@@42334@@@
在单个元素之间有一个 space 分隔符会很棒。有帮助吗?
我将在 python pandas 数据帧 uisng str.replace
函数中使用此正则表达式。
我已经尝试了 regexr.com and regex101.com 并且到目前为止
**编辑:**下面是数据
SNo details
1 account @@@0000082569@@@ / department stores uk & ie credit control operations
2 academic @@@0000060910@@@ , administrative, and @@@0000039198@@@ liaison coordinator
3 account executive, financial @@@0000060910@@@ , enterprise and partner group
4 2015-nasa summer internship- space power system @@@0000129849@@@ and testing
5 account technical @@@0000185187@@@ , technical presales, systems engineer
6 account @@@0000082569@@@ for car, van & 4x4 products in the east of england
7 account @@@0000082569@@@ for mikro segment and owners of the enterprises
8 account @@@0000082569@@@ - affinity digital display, mobile & publishing
9 account @@@0000082569@@@ @@@0000060905@@@ -energy and commodities @@@0000086889@@@ candidate
10 account @@@0000082569@@@ for companies department of external relevance
而不是 replace
和复杂的正则表达式,你可以使用 join
和 findall
并使用更简单的正则表达式,如下所示:
>>> str = 'my name is x and i work at @@@12354@@@ and i am happy with my job. what is your company name? is it @@@42334@@@? you look happy as well!!'
>>> ' '.join(re.findall(r'@{3}\d{1,12}@{3}', str))
'@@@12354@@@ @@@42334@@@'
这就是我在
>>> df = pd.DataFrame({'col1':['at @@@12354@@@ and i am happy with my job. what is your company name? is it @@@42334@@@? you look happy as well!!', 'at @@@222@@@ and t @@@888888@@@?' ]})
>>> df['col1'].str.findall(r'@{3}\d+@{3}').apply(' '.join)
0 @@@12354@@@ @@@42334@@@
1 @@@222@@@ @@@888888@@@
@{3}\d+@{3}
将匹配任何包含 3 个 @
符号的 1+ 数字,.findall
将提取所有匹配项。 .apply(' '.join)
将用 space.