繁体   English   中英

如何在Java中使用正则表达式在句子中间搜索单词

[英]How to search a word in the middle of the sentence using regular expression in java

我有以下句子(字符串)。

服务器-客户端通信中存在连接重置错误。 请更正通讯表名称。xls。

现在在Java中的正则表达式中,如何找到句子开头不存在的“连接重置”。 (我在这里所做的是提取文件名(name.xls),该文件的出现可能会发生“连接重置”。)

我有表达式,如果它在一开始就出现了。

-> 连接重置。*?\\ b([^。] + \\。xls)\\ b

如何修改它以在句子中的任何地方找到“连接重置”。

干得好,

不要用正则表达式转头,

使用以下代码在句子中查找单词,

    String sentence = "There is a Connection reset error in your server-client communication. Please correct the communicating sheet name.xls."

    if(sentence.contains("Connection reset"))
        {
           System.out.println("present");
           String filename = sentence.split("\\s+")[sentence.split("\\s+").length-1];
           System.out.println(filename);
        }
    else
        {
         System.out.println("not present");
        }

也许我误会了一些东西,但是怎么了

.*Connection reset.*?\\b([^ .]+\\.xls)\\b

存在其他可替代女巫是使用梅索德indexOf例如:

 string sentence = "There is a Connection reset error in your server-client 
                    communication. Please correct the communicating sheet."
 if (sentence.indexOf("Connection reset") != -1){ 
     // it return -1 if there is no occurrence .
     System.out.println("exist");
 } else {
     System.out.println("not exist");
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM