Regular Expressions - Logical AND

I know this isn't Java, and it's not really an algorithm, but I can't figure this out. I hope amongst this bright group of developers someone can help me.

I am searching for a regular expressions that will match a series of words.

Example:

Given the words: "ship book"

What regular expression could be used to find both the word "ship" and the "book"?

I have found one expression that will do it ... ship.*book|book.*ship

But that expression doesn't scale. Does anyone know of a better way?

Thanks,

BacMan

[564 byte] By [BacMana] at [2007-9-29 13:23:46]
# 1

Hi,

How about something like:

public class Regexp1

{

private static final Pattern all = Pattern.compile(

"^(\\s*\\b(monkey|turnip|ship|book)\\b\\s*)*$" );

private static final Pattern p = Pattern.compile(

"\\s*\\b(monkey|turnip|ship|book)\\b\\s*" );

public static void main( String[] argv )

{

find( "ship book turnip monkey" );

find( "monkey giraffe mango" );

find( "ship shipship ship" );

}

public static void find( String text )

{

System.out.println( "Text: " + text );

Matcher m = p.matcher( text );

System.out.println( "Matches all: " + all.matcher( text ).matches() );

while ( m.find() )

{

System.out.println( "Matching word: '" + m.group(1) + "'" );

}

}

}

which will produce this when run:

Text: ship book turnip monkey

Matches all: true

Matching word: 'ship'

Matching word: 'book'

Matching word: 'turnip'

Matching word: 'monkey'

Text: monkey giraffe mango

Matches all: false

Matching word: 'monkey'

Text: ship shipship ship

Matches all: false

Matching word: 'ship'

Matching word: 'ship'

Pattern all tests to see if all the words are present.

Pattern p finds each matching word and ignores others.

Ol.

0sa at 2007-7-15 3:38:35 > top of Java-index,Other Topics,Algorithms...
# 2
What are you going to do with the expression? In most cases you could divide it into two expressions. First get all with the word 'book' first, then search only those for the next word.
zisyfosa at 2007-7-15 3:38:35 > top of Java-index,Other Topics,Algorithms...
# 3

another way isimport java.util.*;

class WordList {

public static void main(String [] arg) throws Exception {

List words = new ArrayList() {{add("ship"); add("monkey"); add("air"); add("biscuit");}};

String[] text = {"Air Monkey Shipping Biscuit", "Air Monkey Ship Biscuits", "Monkey Ship Biscuit Air"};

for(int i=0; i<text.length; i++) {

List wordlist = new ArrayList();

for(StringTokenizer st = new StringTokenizer(text[i]); st.hasMoreTokens(); )

wordlist.add(st.nextToken().toLowerCase());

System.out.println(text[i]+" contains all the words? "+wordlist.containsAll(words));

}

}

}

asjf>

asjfa at 2007-7-15 3:38:35 > top of Java-index,Other Topics,Algorithms...
# 4

I guess I should have been more thorough in my explanation of what I am trying to accomplish. My regular expression is in XSLT 2.0. I'm using the Saxon parser (version 7 ... working draft) to match a string given a regular expression.

I was really just looking for the regular expression:

"^(\\s*\\b(monkey|turnip|ship|book)\\b\\s*)*$"

Unfortunatley, I can't get this expression to work. I'm not sure if XSLT supports \b and \s

I think the gist of what you are doing is negating the logical or. I'm going to see what I can come up with. If I find an expression that works I'll give you the duke dollars.

BacMan

BacMana at 2007-7-15 3:38:35 > top of Java-index,Other Topics,Algorithms...
# 5
Here is the solution:(?=.*ship)(?=.*book)
BacMana at 2007-7-15 3:38:35 > top of Java-index,Other Topics,Algorithms...
# 6
Did you intend that to match:"Do the natives worship the kabooks?"^_^
tschodta at 2007-7-15 3:38:35 > top of Java-index,Other Topics,Algorithms...
# 7
yup
BacMana at 2007-7-15 3:38:35 > top of Java-index,Other Topics,Algorithms...