Solved

Regular expression problem.

Posted on 2007-04-11
7
213 Views
Last Modified: 2010-03-31
    Hi!

I need some help with Regular Expressions.
Suppose I have a list/array of words and I want to check a very long string (like a string from an textarea on a webpage)
if any pattern of a particular word in the list/array is in that string.
How can I do it in a fast an efficient way ?

Regards,
  Tomas Helgi
0
Comment
  • 3
  • 2
  • 2
7 Comments
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 18889002
word1|word2|word3
0
 
LVL 25

Author Comment

by:Tomas Helgi Johannsson
ID: 18889192
So I would do something like

Pattern patt = Pattern.compile("word1|word2|word3");
Matcher m = patt.matcher(textareastring);
      while (m.find()) {
        //   do stuff
      }

Now if I would like to check if my name is in that string in some obscure way like
  ....T/oYm/As... but in the array of checklistwords my name would be written normally like Tomas

How would I do that ?

Regards,
  Tomas Helgi
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18889439
You would be better keeping the word list in a text file. Iterate the list and check each for matches - that's all the re engine would do anyway
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 25

Author Comment

by:Tomas Helgi Johannsson
ID: 18889495
I was thinking of having the words in a database table and iterate through that list. But I'm wondering if a text that includes the word "T/oYm/As" would match my word in the table that would be written like "Tomas" ?
Or do I have to set up a pattern like "*T*o*m*a*s*" ?

Regards,
  Tomas Helgi
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18889518
Database would be OK, but harder to change and certain characters could be a problem
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 18889528
>>
But I'm wondering if a text that includes the word "T/oYm/As" would match my word in the table that would be written like
>>

No. You'd need to change the pattern
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 18889608
have you list of words instead be a list of patterns. that would give u the flexibility to handle your matching as required.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now