Multiple OR or AND combination

Find, replace, find in files, replace in files, regular expressions

Multiple OR or AND combination

Postby moguai75 » Tue Sep 14, 2004 5:06 am

How do I do a multiple OR combination?

I want to replace all words in a wordlist that have one, two or three letters.
I tried (^...\r|^..\r|^.\r) in UNIX style, but it only works if I combinate two and not three terms.
User avatar
moguai75
Newbie
 
Posts: 1
Joined: Mon Sep 13, 2004 11:00 pm

Re: Multiple OR or AND combination

Postby Mofi » Tue Sep 14, 2004 9:49 am

Multiple OR combinations are not possible yet in UltraEdit with the UltraEdit or the Unix regular expression engine. Use a macro with a sequence of two or more replaces.

Edited on 2008-10-29: Since UE v12.00 it is possible by using the Perl compatible regular expression engine, see below.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Multiple OR or AND combination

Postby Jane » Tue Oct 28, 2008 12:29 am

If you are using a more recent version which supports Perl regex, i.e. Version 12 or later, you can set it for Perl regular expressions and solve this a few ways.

If you are looking for simple words of 1 to 3 characters, and the words are separated by non-word boundaries you could do it with the regular expression:

Search for:
\b\w{1,3}\b

Replace with nothing

\w will match both letters and numbers. If including numbers is a problem
use:
Search for:
\b[A-Za-z]{1,3}\b

If you were actually needing to select from 3 specific words using OR, you could use the Perl regex construct such as:

(word1|word2|word3)

Cordially,
Jane
User avatar
Jane
Basic User
Basic User
 
Posts: 22
Joined: Sat Aug 05, 2006 11:00 pm
Location: Canada

Re: Multiple OR or AND combination

Postby saltfsh » Mon Jan 25, 2010 12:53 am

Can someone help me with a multiple AND combination?

For example, I don't want to search for either "cat" or "dog" I want to search for them both in the same line. I apologize if I am in the wrong section, but this is the first time I have used the forum.

Thank you for any help
saltfsh
Newbie
 
Posts: 1
Joined: Mon Jan 25, 2010 12:46 am

Re: Multiple OR or AND combination

Postby Mofi » Mon Jan 25, 2010 3:52 am

AND is the default, but the order of the words must match the order of occurence in the file. Unix/Perl regular expression search string cat.*dog finds cat AND dog in this order with 0 or more occurences of any other character between except the new line characters. The UltraEdit regular expression search string for this search is cat*dog.

If you don't know the order of the words in the lines, you need to run either muliple searches with all possible combinations like for this example dog.*cat or use an enhanced Perl expression as posted below by pietzcker which really works independent of the order which finds lines where both words exist on the same line in any order.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4055
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Multiple OR or AND combination

Postby pietzcker » Mon Jan 25, 2010 3:56 am

If you want to match an entire line if and only if it contains both "cat" and "dog", use the following Perl regex:

^(?=.*\bcat\b)(?=.*\bdog\b).*\r\n

Explanation:

^ anchors the regex at the start of the line
(?=.*\bcat\b) asserts that it is possible to match any number of characters, followed by the word "cat" (the \b word boundary anchors ensure that it doesn't match "complicate" oder "catastrophe"). This is called "positive lookahead".
(?=.*\bdog\b) same for the word "dog". If you have more search terms, just add them the same way.
.*\r\n matches the entire line including linebreaks.

Unfortunately, the last line means that the regex will fail if "dog" and "cat" occur on the last line of the file unless that's also CRLF terminated. Correctly, I would have used \r?\n? instead of \r\n to make those linebreaks optional. However, due to a bug in UE's regex engine, this doesn't work (it skips matches in adjacent lines). So either use \r?\n? and apply the regex multiple times in a row, or make sure that the last line of the file is CRLF terminated.
User avatar
pietzcker
Master
Master
 
Posts: 241
Joined: Sun Aug 22, 2004 11:00 pm


Return to Find/Replace/Regular Expressions

cron