开发者

Using regular expressions to find words with @

I am trying to find a word with regular expression, but i don't know how to use it.

I will receive a text and a word and i have to find it in the text wit开发者_StackOverflow中文版h and without @ or #, and return true if i find it or false if dont find.

But it can't be in another word like this: "thisword".

It just can be alone and preceded by # or @.

How can i do this in Java ?

Example:

"the @horse is white"

"the #horse is white"

"the horse is white"

"A shorse is white"

i have to find the word horse, #horse and @horse in this three phrases.


I guess this is what you want:

text.matches("\\b[#@]?" + Pattern.quote(word) + "\\b");


OK...

1) Yes, there's a way. Here, and Here

2) I believe this will help you

public class TestRegex {

public static void main(String[] args) {
    String word = "cat";
    Pattern p = Pattern.compile("\\b[#@]?" + Pattern.quote(word) + "\\b");

    Matcher m = p.matcher("find c@t me @cat #cat c#t cat thiscat");

    while (m.find()) {
        System.out.println(m.group(0));
    }
}

}


Your question is unclear due to lack of examples, however I believe this is what you mean:

str.matches(".*(#|@)");

This matches a string which includes either a # or a @.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜