简体   繁体   English

如果里面的模式匹配,则抓取完整的正则表达式字

[英]Grab full regex word if pattern inside it matches

How do I retrieve an entire word that has a specific portion of it that matches a regex? 如何检索具有与正则表达式匹配的特定部分的整个单词?

For example, I have the below text. 例如,我有以下文字。 Using ^.[\\.\\?\\!:;,]{2,} , I match the first 3, but not the last. 使用^.[\\.\\?\\!:;,]{2,} ,我匹配前3个,但不是最后一个。 The last should be matched as well, but $ doesn't seem to produce anything. 最后一个也应该匹配,但$似乎没有产生任何东西。

a!!!!!!
n.......
c..,;,;,,

huhuhu..

I want to get all strings that have an occurrence of certain characters equal to or more than twice. 我希望得到某些字符出现等于或大于两次的所有字符串。 I produced the aforementioned regex, but on Rubular it only matches the characters themselves, not the entire string. 我制作了前面提到的正则表达式,但在Rubular上它只匹配字符本身,而不是整个字符串。 Using ^ and $ 使用^和$

I've read a few stackoverflow posts similar, but not quite what I'm looking for. 我已经阅读了一些类似的stackoverflow帖子,但不是我想要的。

Change your regex to: 将你的正则表达式改为:

/^.*[.?!:;,]{2,}/gm

ie match 0 more character before 2 of those special characters. 即在其中2个特殊字符之前匹配0个字符。

RegEx Demo RegEx演示

If I understand well you are trying to match an entire string that contains at least the same punctuation character two times: 如果我理解你正在尝试匹配包含至少相同标点字符两次的整个字符串:

^.*?([.?!:;,])\1.*

Note: if your string has newline characters, change .* to [\\s\\S]* 注意:如果您的字符串有换行符,请将.*更改为[\\s\\S]*

The trick is here: 诀窍在于:

([.?!:;,])   # captures the punct character in group 1
\1           # refers to the character captured in group 1

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

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