Solved

# Regular expression problem.

Posted on 2007-04-11
199 Views
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
• 3
• 2
• 2

LVL 92

Accepted Solution

objects earned 500 total points
word1|word2|word3
0

LVL 24

Author Comment

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

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

LVL 24

Author Comment

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

Database would be OK, but harder to change and certain characters could be a problem
0

LVL 86

Expert Comment

>>
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

objects earned 500 total points
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

### Suggested Solutions

Java 1603 Error 2 42
countX 22 69
bitbucket vs gitbucket 3 29
Non-recursive backtracking, using a stack 1 48
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop: