Page 1 of 1

Regex Search dummie question

Posted: 10 Oct 2012 12:29
by David.P
Hi forum,

just a simple question (hopefully): How can I search files contents for something like

Code: Select all

(Peter OR Paul) AND Martha
Will I have to use Regex, and if yes, what would be the syntax?

Second, is it possible to search in "Office" AND "Text" files simultaneously?

Thanks heaps already,

Cheers David.P

Re: Regex Search dummie question

Posted: 10 Oct 2012 13:38
by highend
Are the names on the same line?
E.g. this regex would find files that contain lines like:

Paul searches for Martha
Peter loves Martha
etc.

Code: Select all

(Peter|Paul)(?=.*Martha)
If you want to look it up:
| = or
(?=.*text) = positive lookahead, .* takes anything before "text" appears

Re: Regex Search dummie question

Posted: 10 Oct 2012 16:28
by David.P
Thanks highend,
highend wrote:Are the names on the same line?
No, just anywhere in the file!

Is it possible to carry out this search?

Re: Regex Search dummie question

Posted: 10 Oct 2012 18:49
by highend
This could work:

Code: Select all

(Peter|Paul).*[\s|\S](?=.*Martha)

Re: Regex Search dummie question

Posted: 12 Oct 2012 02:01
by highend
So, does that work for you?

Re: Regex Search dummie question

Posted: 12 Oct 2012 03:56
by xman
highend wrote:This could work:

Code: Select all

(Peter|Paul).*[\s|\S](?=.*Martha)
This is I believe imperfect (tested it). It will match Martha only on current line or the next one, but not anywhere further. Also, character | doesn't belong here: [\s|\S], unless you want to match "|".

This will locate Martha anywhere, ignoring any number of newlines and other characters:

Code: Select all

(Peter|Paul)[\s\S]*?Martha
Or alternatively, using lookahead:

Code: Select all

(Peter|Paul)(?=[\s\S]*?Martha)
Character ? is there so it will not search throught the entire text, but just till the next occurence.

And this will locate Martha after OR before Peter/Paul, which is what OP requested:

Code: Select all

((Peter|Paul)[\s\S]*?Martha|Martha[\s\S]*?(Peter|Paul))

Re: Regex Search dummie question

Posted: 12 Oct 2012 08:27
by David.P
This is great, thanks guys -- but OMG!, is there no simpler way to do a Boolean search?

Re: Regex Search dummie question

Posted: 12 Oct 2012 15:07
by xman
Probably not :/

Also, as per the help file, there is this:

Code: Select all

Note: Since larger files are read chunkwise (performance!) there are certain limits to the scope of your pattern: A pattern is  guaranteed to be found if it matches a range of 1,024 bytes! It also *might* be found if it matches a range of 1,025 to 32,768 bytes! It will certainly not be found if it matches only an even larger range.

Re: Regex Search dummie question

Posted: 12 Oct 2012 15:54
by David.P

Re: Regex Search dummie question

Posted: 12 Oct 2012 17:16
by highend
Maybe it's only supported for name & location?

At least it doesn't seem to work in the content field.

@xman
Thanks for correcting the necessary regex.