Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. Using ^ and $

I've read a few stackoverflow posts similar, but not quite what I'm looking for.

like image 299
hansvimmer Avatar asked Jan 30 '26 17:01

hansvimmer


2 Answers

Change your regex to:

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

i.e. match 0 more character before 2 of those special characters.

RegEx Demo

like image 139
anubhava Avatar answered Feb 01 '26 06:02

anubhava


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]*

The trick is here:

([.?!:;,])   # captures the punct character in group 1
\1           # refers to the character captured in group 1
like image 37
Casimir et Hippolyte Avatar answered Feb 01 '26 07:02

Casimir et Hippolyte



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!