Java正则表达式不会查找第一个单词之外的其他单词

huangapple go评论87阅读模式
英文:

Java regex won't look for words other than the first one

问题

这部分代码的工作原理是,用户将文本粘贴到命令行中,然后可以选择其中一个程序选项。其中之一是在文本中查找特定单词/短语。以下是我为这个部分编写的代码:

public void searching() {
    System.out.println("请输入您要查找的单词");
    Pattern pattern = Pattern.compile(scan.next());
    Matcher matcher = pattern.matcher(text);

    boolean found = false;
    while (matcher.find()) {
        System.out.println("我在索引 " + matcher.start() + " 处找到文本 \"" + text.substring(matcher.start(), matcher.end()) + "\"");
        found = true;
    }
    if (!found) {
        System.out.println("未找到匹配项。");
    }
}

我的问题是,如果用户的文本例如为"lorem ipsum dolor sit amet",他们想要找到单词"dolor"的位置,输出会是"未找到匹配项"。该程序只会在用户要求查找"lorem"时才会找到。

另一个问题是:是否有办法使输出不是找到的单词的索引,而是整个文本和找到的单词,且单词为大写?

英文:

The way it works is, that the user will paste a text into the cmd and then he can choose one of the program options. One of them is find specific word/phrase in text. This is the code I have for this section:

	public void searching() {
    
    System.out.println("Enter the word you wish to find");
    Pattern pattern = Pattern.compile(scan.next());
    Matcher matcher = pattern.matcher(text);
    
    boolean found = false;
    while (matcher.find()) {
          System.out.println("I found the text "+pattern+" starting at index "+    
             matcher.start()+" and ending at index "+matcher.end());    
            found = true;    
        }    
        if(!found){    
            System.out.println("No match found.");    
    }
}

My problem is that if the user's text is for example "lorem ipsum dolor sit amet" and they want to find where the word "dolor" is, the output is "no match found". The program will only find the word lorem if the user asks for it.

The other question is: is there a way to make the output be not the index of the found word but the whole text and the found word in the upper case?

答案1

得分: 2

你已经做到了您粘贴的代码正常运行自包含测试案例

Pattern pattern = Pattern.compile("dolor");
Matcher matcher = pattern.matcher("lorem ipsum dolor sit amet");
boolean found = false;
while (matcher.find()) {
  System.out.println("我发现了文本 " + pattern + " 从索引 " +
    matcher.start() + " 开始,到索引 " + matcher.end());
  found = true;
}
if (!found) {
  System.out.println("未找到匹配。");
}

它会输出

> `我发现了文本 dolor 从索引 12 开始到索引 17`

您可以使用索引编写一些代码并操作字符串您要查找

* `substring` - 切片字符串。`"hello".substring(1, 4)` 变为 `"ell"
* `toUpperCase()` - 将文本转换为大写。`"hello".toUpperCase()` 变为 "HELLO"
* 连接字符串只需使用 +。`"he" + "ll" + "o"` 变为 "HELLO"

这些操作是您需要的全部操作

首先创建3个子字符串`matcher.start()` 之前在 start 和 end 之间以及在 end 之后然后将中间的子字符串转换为大写然后将它们全部连接起来如果您想要将所有匹配项都转换为大写可以使用类似的技术也许您想要使用 `StringBuilder`,它允许您逐步构建字符串

StringBuilder x = new StringBuilder();
x.append("he");
x.append("ll");
x.append("o");
String y = x.toString();
System.out.println(y); // 输出 "hello";
英文:

You're doing it. Your code, as pasted, works fine. Self-contained test-case:

Pattern pattern = Pattern.compile("dolor");
Matcher matcher = pattern.matcher("lorem ipsum dolor sit amet");
boolean found = false;
while (matcher.find()) {
System.out.println("I found the text "+pattern+" starting at index "+    
matcher.start()+" and ending at index "+matcher.end());    
found = true;    
}    
if(!found) {
System.out.println("No match found.");    
}

and it prints:

> I found the text dolor starting at index 12 and ending at index 17

You can use the index to then write some code and manipulate the string. You're looking for:

  • substring - slices strings up. "hello".substring(1, 4) becomes `"ell".
  • toUpperCase() - uppercases text. "hello".toUpperCase() becomes "HELLO"
  • concatenate strings. Just + will do. "he" + "ll" + "o" becomes "HELLO"

These operations are all you need.

first make 3 substrings: before the 'matcher.start()', between start and end, and after end. Then uppercase the middle one. Then concatenate them all. If you want to uppercase all the matches, similar techniques. Maybe you want to involve StringBuilder which lets you build up the string piece by piece:

StringBuilder x = new StringBuilder();
x.append("he");
x.append("ll");
x.append("o");
String y = x.toString();
System.out.println(y); // prints "hello"

huangapple
  • 本文由 发表于 2020年9月29日 20:34:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64119749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定