find a string in line

P

puzzlecracker

you have a sentence and you need to find wether it contains a word; for
example "he is great " .containts("is") returns true, but "his
great".contains("is") also returns true, but I want it to return false


I tried regexpe but it didnt work String
patterns="[^A-Za-z]+str+[^A-Za-z]" ?

but it didnt work.
 
Z

zero

you have a sentence and you need to find wether it contains a word; for
example "he is great " .containts("is") returns true, but "his
great".contains("is") also returns true, but I want it to return false


I tried regexpe but it didnt work String
patterns="[^A-Za-z]+str+[^A-Za-z]" ?

but it didnt work.

Use the whitespace character class:

".*\\sis\\s.*"

will match zero or more characters, a whitespace, the string "is", a
whitespace, and zero or more characters.

Note the double backslashes.
 
P

puzzlecracker

zero said:
you have a sentence and you need to find wether it contains a word; for
example "he is great " .containts("is") returns true, but "his
great".contains("is") also returns true, but I want it to return false


I tried regexpe but it didnt work String
patterns="[^A-Za-z]+str+[^A-Za-z]" ?

but it didnt work.

Use the whitespace character class:

".*\\sis\\s.*"

will match zero or more characters, a whitespace, the string "is", a
whitespace, and zero or more characters.

Note the double backslashes.

but I also want it to match things such as ,is, is.
 
P

puzzlecracker

zero said:
you have a sentence and you need to find wether it contains a word; for
example "he is great " .containts("is") returns true, but "his
great".contains("is") also returns true, but I want it to return false


I tried regexpe but it didnt work String
patterns="[^A-Za-z]+str+[^A-Za-z]" ?

but it didnt work.

Use the whitespace character class:

".*\\sis\\s.*"

will match zero or more characters, a whitespace, the string "is", a
whitespace, and zero or more characters.

Note the double backslashes.

pattern=".*[^a-z]" +"is"+"[^a-z].*";

line.matches(patern);

returns few results

Note: both line and is are lower case.
 
Z

zero

but I also want it to match things such as ,is, is.

Then maybe the \W construct can help. This will match any non-word
character.

".*\\W+" + searchString + "\\W+.*"

non-word characters are defined as anything other than an alfanumeric
character or an underscore. So this would return true for "what is, once
was." but not for "his word"

There may be a problem if the search string is at the end or beginning
(or both) of the line you're searching, but you can check for that with
String:startsWith and String:endsWith
 
N

Noodles Jefferson

puzzlecracker took the hamburger, threw it on the grill, and I said "Oh
wow"...
you have a sentence and you need to find wether it contains a word; for
example "he is great " .containts("is") returns true, but "his
great".contains("is") also returns true, but I want it to return false


I tried regexpe but it didnt work String
patterns="[^A-Za-z]+str+[^A-Za-z]" ?

but it didnt work.

Not Tested:

public boolean findAWord(String aString, String aWord) {

boolean hasWord = false;
int i = 0;

String[] sa = aString.split();

while (i < sa.length) {

if (sa.equals(aWord)) {

hasWord = true;
break;

}

i++;

}

return hasWord;

}

--
Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "The Road to Chicago" -- Thomas Newman (Road to Perdition
Soundtrack)

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.
 
P

puzzlecracker

public boolean findAWord(String aString, String aWord) {

boolean hasWord = false;
int i = 0;

String[] sa = aString.split();

while (i < sa.length) {

if (sa.equals(aWord)) {

hasWord = true;
break;

}

i++;

}


Not going to work, it will only find word delemited by space such as
".. word .." but not ",word"
 
P

puzzlecracker

puzzlecracker said:
public boolean findAWord(String aString, String aWord) {

boolean hasWord = false;
int i = 0;

String[] sa = aString.split();

while (i < sa.length) {

if (sa.equals(aWord)) {

hasWord = true;
break;

}

i++;

}


Not going to work, it will only find word delemited by space such as
".. word .." but not ",word"
return hasWord;

}

--
Noodles Jefferson
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "The Road to Chicago" -- Thomas Newman (Road to Perdition
Soundtrack)

"Our earth is degenerate in these latter days, bribery and corruption
are common, children no longer obey their parents and the end of the
world is evidently approaching."
--Assyrian clay tablet 2800 B.C.


didn't work either
pattern=".*\\W?"+s+"\\W.*?";
 
C

Chris Smith

zero said:
Then maybe the \W construct can help. This will match any non-word
character.

".*\\W+" + searchString + "\\W+.*"

Just to be paranoid, make that:

".*\\W+" + Pattern.quote(searchString) + "\\W+.*"

Note that Pattern.quote is only available in Java 1.5. Prior to Java
1.5, it's exceedingly difficult to search for arbitrary substrings using
regular expressions, and you'd be better of using String.indexOf(String)
and checking the surrounding characters on your own.
There may be a problem if the search string is at the end or beginning
(or both) of the line you're searching, but you can check for that with
String:startsWith and String:endsWith

Or, since you've got a Java regular expression anyway:

"(^|.*\\W+)" + Pattern.quote(searchString) + "($|\\W+.*)"

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
P

puzzlecracker

Chris said:
Just to be paranoid, make that:

".*\\W+" + Pattern.quote(searchString) + "\\W+.*"

Note that Pattern.quote is only available in Java 1.5. Prior to Java
1.5, it's exceedingly difficult to search for arbitrary substrings using
regular expressions, and you'd be better of using String.indexOf(String)
and checking the surrounding characters on your own.


Or, since you've got a Java regular expression anyway



that is slow with JFC... any old fashion ways to accomplish this task?

Thanks
 
P

puzzlecracker

Chris said:
Just to be paranoid, make that:

".*\\W+" + Pattern.quote(searchString) + "\\W+.*"

Note that Pattern.quote is only available in Java 1.5. Prior to Java
1.5, it's exceedingly difficult to search for arbitrary substrings using
regular expressions, and you'd be better of using String.indexOf(String)
and checking the surrounding characters on your own.


Or, since you've got a Java regular expression anyway:

"(^|.*\\W+)" + Pattern.quote(searchString) + "($|\\W+.*)"

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

regular expressiona are quite slow with jfc.... can anyone suggest a
quick indexof variant or lagacy variant?
 
C

Chris Smith

puzzlecracker said:
regular expressiona are quite slow with jfc.... can anyone suggest a
quick indexof variant or lagacy variant?

Huh? Is this some "jfc" that I'm not familiar with? Regular
expressions are no slower or faster than normal with the Java Foundation
Classes (that is, Swing and some related APIs). In fact, the two have
little to do with each other.

In any case, Noodles Jefferson already gave you a solution without using
regular expressions. You seemed no happier with that, because it didn't
work precisely the way you want. If you're that unable to write your
own code, perhaps its time to think about why you're involved in
programming. An if statement probably won't kill you.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
J

Jaakko Kangasharju

zero said:
Then maybe the \W construct can help. This will match any non-word
character.

".*\\W+" + searchString + "\\W+.*"

non-word characters are defined as anything other than an alfanumeric
character or an underscore. So this would return true for "what is, once
was." but not for "his word"

There may be a problem if the search string is at the end or beginning
(or both) of the line you're searching, but you can check for that with
String:startsWith and String:endsWith

You can use boundary matches to overcome this problem. The \b
construct matches a word boundary, so modifying your expression to
".*\\b" + searchString + "\\b.*" matches searchString between word
boundaries, including at the beginning or the end.
 
C

Chris Smith

Jaakko Kangasharju said:
You can use boundary matches to overcome this problem. The \b
construct matches a word boundary, so modifying your expression to
".*\\b" + searchString + "\\b.*" matches searchString between word
boundaries, including at the beginning or the end.

So if that works, then the correct version can be written as:

".*\\b" + Pattern.quote(searchString) + "\\b.*"

The Pattern.quote could technically be omitted if searchString were
guaranteed to contain only word characters... but it would need to be
accompanied with copious documentation to explain that fact.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
Z

zero

So if that works, then the correct version can be written as:

".*\\b" + Pattern.quote(searchString) + "\\b.*"

The Pattern.quote could technically be omitted if searchString were
guaranteed to contain only word characters... but it would need to be
accompanied with copious documentation to explain that fact.

It seems patterns are indeed a complex subject, with lots of near-identical
alternatives. Btw, anyone know how this works with strings with
international content? The Pattern JavaDoc states that a word character is
[a-ZA-Z_0-9], so accented characters won't work here - and I'm not even
talking about non-latin script.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top