Capture the full word of the regex if the pattern inside it matches

How do I get a whole word that has a specific portion of it that matches a regular expression?

For example, I have the text below. Using ^.[\.\?\!:;,]{2,}

, I am matching the first 3, but not the last. The latter should also be matched, but the $ doesn't seem to produce anything.

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

huhuhu..

      

I want to get all lines that have the occurrence of certain characters equal to or more than two times. I have produced the above regex, but in Rubular it only matches characters and not the whole string. Using ^ and $

I have read several stackoverflow posts that are similar, but not exactly what I'm looking for.

+3


source to share


2 answers


Change the regex to:

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

      



i.e. match 0 characters to 2 of these special characters.

Demo version of RegEx

+1


source


If I understand well, you are trying to match up a whole string containing at least the same punctuation character twice:

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

      

Note: if your string has newlines, change .*

to[\s\S]*



The trick is here:

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

      

+1


source







All Articles