Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match when a string is present twice

Tags:

I am horrible at RegEx expressions and I just don't use them often enough for me to remember the syntax between uses.

I am using grepWin to search my files. I need to do a search that will return the files that have a given string twice.

So, for example, if I was searching on the word "how", then file one would not match:

Hello
how are you today?

but file two would:

Hello
how are you today?

I am fine, how are you?

Any one know how to make a RegEx that will match that?

like image 795
Vaccano Avatar asked Nov 18 '11 17:11

Vaccano


People also ask

How do you find multiple occurrences of a string in regex?

Method 1: Regex re. To get all occurrences of a pattern in a given string, you can use the regular expression method re. finditer(pattern, string) . The result is an iterable of match objects—you can retrieve the indices of the match using the match.

How do you repeat in regex?

A repeat is an expression that is repeated an arbitrary number of times. An expression followed by '*' can be repeated any number of times, including zero. An expression followed by '+' can be repeated any number of times, but at least once.

What does two backslashes mean in regex?

Similarly, a single backslash marks the beginning of an escaped language construct, but two backslashes ( \\ ) indicate that the regular expression engine should match the backslash.

What does \\ mean in regex?

\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \. html?\ ' .


1 Answers

something like this (depends on language and your specific task)

\(how.*){2}\ 

Edit: according to @CodeJockey

\^(([^h]|h[^o]|ho[^w])*how([^h]|h[^o]|ho[^w])*){2,2}$\ 

(it become more complicated) @CodeJockey: Thanks for comments

like image 79
VMykyt Avatar answered Oct 21 '22 00:10

VMykyt