variables in regex's

V

vivekhungund

Hi, I was wondering how to incorporate a string variable into a
regex. So I've got an array of rules "S -> NP VP" "NP -> dog" etc.,
and a sentence "the dog is blue". I want to match each word in the
sentence to the corresponding rule in my array.
Say the rule "NP -> dog" is myArray[1]. I want something like:
Pattern pattern = Pattern.compile("\bmyArray[1]\b")

I'd appreciate any advice.

Thanks.
 
H

hiwa

Hi, I was wondering how to incorporate a string variable into a
regex. So I've got an array of rules "S -> NP VP" "NP -> dog" etc.,
and a sentence "the dog is blue". I want to match each word in the
sentence to the corresponding rule in my array.
Say the rule "NP -> dog" is myArray[1]. I want something like:
Pattern pattern = Pattern.compile("\bmyArray[1]\b")

I'd appreciate any advice.

Thanks.
Pattern pattern = Pattern.compile("\\b" + myArray[1] + "\\b");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top