Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 有没有一种更有效的方法可以在编译之前在不知道关键字的情况下搜索句子中的关键字?_Java_Search - Fatal编程技术网

Java 有没有一种更有效的方法可以在编译之前在不知道关键字的情况下搜索句子中的关键字?

Java 有没有一种更有效的方法可以在编译之前在不知道关键字的情况下搜索句子中的关键字?,java,search,Java,Search,我是一名计算机科学专业的二年级学生,我编写了一个程序,可以连接到IRC客户端,嗅探等待用户询问“什么是@TWITTERHANDLE推特?” 程序嗅探关键字“tweeting”,然后运行一个方法,该方法将使用整个输入行查找twitter句柄 问题是,如果我在编译之前知道关键字是什么,我就知道如何查找关键字,但在这种情况下,关键字可能是任何twitter句柄,因此我不确定搜索关键字的最佳方法是什么 我尝试了以下操作:inputLine.substring(I+1,inputLine.indexOf(

我是一名计算机科学专业的二年级学生,我编写了一个程序,可以连接到IRC客户端,嗅探等待用户询问“什么是@TWITTERHANDLE推特?”

程序嗅探关键字“tweeting”,然后运行一个方法,该方法将使用整个输入行查找twitter句柄

问题是,如果我在编译之前知道关键字是什么,我就知道如何查找关键字,但在这种情况下,关键字可能是任何twitter句柄,因此我不确定搜索关键字的最佳方法是什么

我尝试了以下操作:inputLine.substring(I+1,inputLine.indexOf(“”));但是它会使用inputLine中的第一个空格作为第二个索引,给我一个IndexOutOfBounds异常

输入行的示例如下所示

:TWalk!46774664@gateway/web/freenode/ip.11.222.33.444 PRIVMSG #irchacks :What is @TWalkBot tweeting about right now?
这就是我的代码,它实际上会抓住twitter的手柄

/** This method parses the inputLine and returns the Twitter Handle */
    public String getTwitterHandle(String inputLine) {

        boolean firstAt = true;
        char currentChar, tempChar;

        for (int i = 0; i < inputLine.length(); i++) {

            currentChar = inputLine.charAt(i);

            if (currentChar == '@' && firstAt == false) {

                for (int j = i; j < inputLine.length(); j++) {

                    tempChar = inputLine.charAt(j);

                    if (tempChar == ' ') {

                        return inputLine.substring(i + 1, j);

                    }

                }

            }

            else if (currentChar == '@' && firstAt == true) {

                firstAt = false;
            }

        }

        return "User Not Found";
    }
/**此方法解析输入行并返回Twitter句柄*/
公共字符串getTwitterHandle(字符串输入行){
布尔值firstAt=true;
char currentChar,tempChar;
对于(int i=0;i
虽然这段代码确实有效,但我想知道是否有更有效的方法来实现这一点,如果是这样,我应该如何看待更改

谢谢


Thomas

是的,您可以使用正则表达式使其变得简单和准确:

public class RegExPattern {

    public static void main(String[] args) {
        String test1 = "What is @TWITTERHANDLE tweeting about?";
        String test2 = ":TWalk!46774664@gateway/web/freenode/ip.11.222.33.444 PRIVMSG #irchacks :What is @TWalkBot tweeting about right now?";

        Pattern pattern = Pattern.compile(".*@(\\w+).*tweeting.*");
        Matcher matcher = pattern.matcher(test1);
        if(matcher.find()){
            System.out.println(matcher.group(1)); //Sysouts TWITTERHANDLE
        }

        matcher = pattern.matcher(test2);
        if(matcher.find()){
            System.out.println(matcher.group(1)); //Sysouts TWalkBot
        }
    }
}
关于正则表达式的解释

*@(\\w+).*推特。*


*@
-贪婪模式匹配字符串,直到最后出现@符号

(\\w+)
-创建组1,后跟@symbol,这是我们在这里的目标,查找长度为1或更长的单词字符

*tweeting.
-查找tweeting单词出现在组1之后,第一个贪婪的
*
模式再次用于说明组1和tweeting之间的任何字符,第二个模式用于说明tweeting之后的任何尾随字符


这件可能更适合你,谢谢。你能确切地解释一下匹配器和模式的作用吗?我很难理解这种模式的含义。这些都是很好的教程——我在互联网上找到的,这将给你们一个入门的机会。检查一下,如果你还有问题,请告诉我。