richardsimnett
asked on
What would the regex be to see if any of the words in a list exist in a line of text?
Hello,
What would the regex look like to see if any of the following words appeared in a line of text?
the, at, are, is, will
I just need something that will return true if any of those words are present. Im not familiar with regex at all so I was hoping someone could show me how to do it.
Thanks,
Rick
What would the regex look like to see if any of the following words appeared in a line of text?
the, at, are, is, will
I just need something that will return true if any of those words are present. Im not familiar with regex at all so I was hoping someone could show me how to do it.
Thanks,
Rick
ASKER
ddrudik,
Is it safe to assume that the raw match pattern will work with String.matches() ?
Thanks,
Rick
Is it safe to assume that the raw match pattern will work with String.matches() ?
Thanks,
Rick
ASKER
also is that pattern case insensitive?
No, the raw match pattern is not formatted properly for use in Java code, see the code example for the formatting as it should be (at least how I understand it should be).
ASKER
ok... what I need to be able to do is this
function boolean lineTest(String line)
{
return ((line.substring(3) == "CHK") && (line.matches("someregex") == false));
}
I need some regex I can place into the line.matches statement that will return true if any of the words in the regex match, and it needs to be case insensitive matching because line can be upper or lower case, or contain any combination of upper or lower case.
function boolean lineTest(String line)
{
return ((line.substring(3) == "CHK") && (line.matches("someregex")
}
I need some regex I can place into the line.matches statement that will return true if any of the words in the regex match, and it needs to be case insensitive matching because line can be upper or lower case, or contain any combination of upper or lower case.
To use .matches I would recommend you try the pattern:
"^.*\\b(?:the|at|are|is|wi ll)\\b.*$"
you could leave the ^ and $ off of the pattern since with .matches it is assumed:
".*\\b(?:the|at|are|is|wil l)\\b.*"
"^.*\\b(?:the|at|are|is|wi
you could leave the ^ and $ off of the pattern since with .matches it is assumed:
".*\\b(?:the|at|are|is|wil
As for case-insensitive matching you cannot specify that with .matches, you will need to convert "line" to lowercase before sending to .matches.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
Thanks for the question and the points.
Open in new window