42

I'm trying to find all pages which contain words "text1" and "text2". My regex:

text1(.|\n)*text2 

it doesn't work.. enter image description here

DmitMedv
  • 980
  • 3
  • 11
  • 22

5 Answers5

52

If your IDE supports the s (single-line) flag (so the . character can match newlines), you can search for your items with:

(text1).*(text2)|\2.*\1

Example with s flag

If the IDE does not support the s flag, you will need to use [\s\S] in place of .:

(text1)[\s\S]*(text2)|\2[\s\S]*\1

Example with [\s\S]

Some languages use $1 and $2 in place of \1 and \2, so you may need to change that.

EDIT:

Alternately, if you want to simply match that a file contains both strings (but not actually select anything), you can utilize look-aheads:

(?s)^(?=.*?text1)(?=.*?text2)

This doesn't care about the order (or number) of the arguments, and for each additional text that you want to search for, you simply append another (?=.*?text_here). This approach is nice, since you can even include regex instead of just plain strings.

OnlineCop
  • 4,019
  • 23
  • 35
  • 1
    (?s) is an inline way to use the 's' flag. If you can't use it, replace the '.' with [\S\s] or any other [\W\w] or [\D\d] variation that you prefer. – OnlineCop Nov 12 '14 at 16:20
  • 1
    [\s\S] it works and that's what I was looking for! Thnx! – DmitMedv Nov 13 '14 at 07:36
  • 1
    IntelliJ does not support single-line or I did not find to switch it to that mode – user637338 Jul 30 '15 at 13:12
  • 3rd one crashed Xcode :D. 2nd one makes Xcode unresponsive and I have to force quit it after a few minutes. 1st suggestion doesn't do anything. – mfaani Feb 02 '17 at 20:55
  • @Honey Exactly how far apart are your `text1` and `text2`? A few lines? A few paragraphs? One at the top and one at the bottom of the document? Do you use `*?` instead of `*`? – OnlineCop Feb 02 '17 at 22:41
  • Hmmm. I see. Far apart top bottom. Your question about which of the 3 options. I dont get it. I just used all the ways you suggested by replacing text1,2 – mfaani Feb 02 '17 at 22:53
  • But text1 needs to appear before text2 in the file. But the order shouldn't matter. So technically this doesn't answers the question. – Chirag Arora Feb 04 '20 at 08:55
  • @ChiragArora Please explain. Why should text1 appear before text2 if "the order shouldn't matter"? If the order matters, use the original suggestion. If the order does not matter, use the look-ahead suggestion (after the EDIT), as all they do is assert whether the regex within them succeeds. – OnlineCop Feb 04 '20 at 20:15
21
text0[\s\S]*text1

Try this.This should do it for you.

What this does is match all including multiline .similar to having .*? with s flag.

\s takes care of spaces,newlines,tabs \S takes care any non space character.

vks
  • 67,027
  • 10
  • 91
  • 124
3

If you want the regex to match over several lines I would try:

text1[\w\W]*text2

Using . is not a good choice, because it usually doesn't match over multiple lines. Also, for matching single characters I think using square brackets is more idiomatic than using ( ... | ... )

If you want the match to be order-independent then use this:

(?:text1[\w\W]*text2)|(?:text2[\w\W]*text1)
Jonathan Benn
  • 2,908
  • 4
  • 24
  • 28
1

Adding a response for IntelliJ

Building on @OnlineCop's answer, to swap the order of two expressions in IntelliJ,you would style the search as in the accepted response, but since IntelliJ doesn't allow a one-line version, you have to put the replace statement in a separate field. Also, IntelliJ uses $ to identify expressions instead of \.

For example, I tend to put my nulls at the end of my comparisons, but some people prefer it otherwise. So, to keep things consistent at work, I used this regex pattern to swap the order of my comparisons:

enter image description here

Notice that IntelliJ shows in a tooltip what the result of the replacement will be.

Jonathan E. Landrum
  • 2,748
  • 4
  • 30
  • 46
  • how to add multi line search? see two search boxes but don't know how you did that – Collin Aug 27 '22 at 00:45
  • @CollinFox There are two separate search boxes because I was doing a search with replace. The expression will find all instances that match the expression, which is why it highlighted multiple lines. – Jonathan E. Landrum Aug 28 '22 at 02:49
0

For me works text1*{0,}(text2){0,}.

With {0,} you can decide to get your keyword zero or more times OR you set {1,x} to get your keyword 1 or x-times (how often you want).

Wollhaar
  • 182
  • 3
  • 22