在文本文件中搜索子字符串 - java
Searching for substrings in a text file - java
我正在做一项繁琐的作业,但我卡在了一个我认为相当简单的部分。我需要在文本文件中搜索 'names'。名称之前用连字符表示,例如
"Hello this is an example, if we were to use -john, then john would be the name I would be looking for"
在这种情况下,我需要获取 'John' 并将其存储到列表中。
我知道如何使用 java 的 fileReader 和 readLine 函数读取行..但我不知道如何在文本文件中查找字符,比如搜索连字符然后创建一个简短的连字符后的子字符串和下一个 space 表示名称。有人可以帮助我使用伪代码或我不知道的功能来使这更容易吗?如果我听起来很混乱,我深表歉意!
我建议使用模式和匹配器来解决问题。
public class Example {
public static void main(String[] args) {
String test = "Hello this is an example, if we were to use -john, then john would be the name I would be looking for. Another example would be -ebenezer which should return ebenezer";
Pattern pattern = Pattern.compile("-(\w+)");
Matcher matcher = pattern.matcher(test);
while (matcher.find()){
System.out.println(matcher.group());
}
}
}
这导致:
-john
-ebenezer
请注意,这不会捕获任何换行到下一行的匹配项。
String line;
List<String> list = new ArrayList();
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileToSearch));
while((line = bufferedReader.readLine()) != null) {
arr[] = line.split(" ");
for (int i = 0; i < arr.length; i++) {
if(arr[i].contains("\"))
list.add(arr[i].substring(arr[i].indexOf("\") + 1);
}
return list;
有多种方法可以解决这个问题。如果您了解正则表达式,可能最简单的方法是使用 Java 的 Pattern
和 Matcher
.
List<String> lines = Files.readAllLines(Paths.get("names.txt")); //use path to your file
final List<String> names = new ArrayList<>();
for (String line : lines) {
Pattern pattern = Pattern.compile("\s(-\w+)\s*");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
String name = matcher.group(1);
//optionally, remove the -
name = name.substring(1);
names.add(name);
}
}
System.out.println(Arrays.toString(names.toArray()));
另一个解决方案
try (BufferedReader reader = new BufferedReader(new FileReader(new File("test.txt")))) {
int c;
StringBuilder nameBuilder = new StringBuilder();
boolean inName = false;
while ((c = reader.read()) != -1) {
if (inName) nameBuilder.append((char)c);
if ((char) c == '-') inName = true;
else if (inName && (char) c == ' ') {
inName = false;
System.out.println(nameBuilder);
nameBuilder.delete(0, nameBuilder.length());
}
}
} catch (IOException e) {
e.printStackTrace();
}
您可以在行中使用正则表达式来查找字符串中的名称。正则表达式用于查找和匹配给定字符串中的某些特征。
因此对于您的示例,使用反斜杠,您将使用以下代码:
Pattern p = Pattern.compile("-.");
Matcher m = p.matcher("\Tom");
if (m.find()) {
System.out.println(m.group());
}
“-”。表示紧跟在“\”之后的任何字符的模式。注意,因为'\'是转义字符,所以它前面必须有另一个'\'。
如果你想让它只接受字母表中的字符,你可以使用“-/^[A-z]+$/”,它基本上只查找直接跟在字母表中的字符的连字符。
我建议您仔细阅读正则表达式,以及它提供的不同表达式。
此外,学习正则表达式的一个很好的资源是 Regexr.com,它允许您查找正则表达式参考,并在线尝试各种表达式。
我正在做一项繁琐的作业,但我卡在了一个我认为相当简单的部分。我需要在文本文件中搜索 'names'。名称之前用连字符表示,例如
"Hello this is an example, if we were to use -john, then john would be the name I would be looking for"
在这种情况下,我需要获取 'John' 并将其存储到列表中。
我知道如何使用 java 的 fileReader 和 readLine 函数读取行..但我不知道如何在文本文件中查找字符,比如搜索连字符然后创建一个简短的连字符后的子字符串和下一个 space 表示名称。有人可以帮助我使用伪代码或我不知道的功能来使这更容易吗?如果我听起来很混乱,我深表歉意!
我建议使用模式和匹配器来解决问题。
public class Example {
public static void main(String[] args) {
String test = "Hello this is an example, if we were to use -john, then john would be the name I would be looking for. Another example would be -ebenezer which should return ebenezer";
Pattern pattern = Pattern.compile("-(\w+)");
Matcher matcher = pattern.matcher(test);
while (matcher.find()){
System.out.println(matcher.group());
}
}
}
这导致:
-john
-ebenezer
请注意,这不会捕获任何换行到下一行的匹配项。
String line;
List<String> list = new ArrayList();
BufferedReader bufferedReader = new BufferedReader(new FileReader(fileToSearch));
while((line = bufferedReader.readLine()) != null) {
arr[] = line.split(" ");
for (int i = 0; i < arr.length; i++) {
if(arr[i].contains("\"))
list.add(arr[i].substring(arr[i].indexOf("\") + 1);
}
return list;
有多种方法可以解决这个问题。如果您了解正则表达式,可能最简单的方法是使用 Java 的 Pattern
和 Matcher
.
List<String> lines = Files.readAllLines(Paths.get("names.txt")); //use path to your file
final List<String> names = new ArrayList<>();
for (String line : lines) {
Pattern pattern = Pattern.compile("\s(-\w+)\s*");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
String name = matcher.group(1);
//optionally, remove the -
name = name.substring(1);
names.add(name);
}
}
System.out.println(Arrays.toString(names.toArray()));
另一个解决方案
try (BufferedReader reader = new BufferedReader(new FileReader(new File("test.txt")))) {
int c;
StringBuilder nameBuilder = new StringBuilder();
boolean inName = false;
while ((c = reader.read()) != -1) {
if (inName) nameBuilder.append((char)c);
if ((char) c == '-') inName = true;
else if (inName && (char) c == ' ') {
inName = false;
System.out.println(nameBuilder);
nameBuilder.delete(0, nameBuilder.length());
}
}
} catch (IOException e) {
e.printStackTrace();
}
您可以在行中使用正则表达式来查找字符串中的名称。正则表达式用于查找和匹配给定字符串中的某些特征。
因此对于您的示例,使用反斜杠,您将使用以下代码:
Pattern p = Pattern.compile("-.");
Matcher m = p.matcher("\Tom");
if (m.find()) {
System.out.println(m.group());
}
“-”。表示紧跟在“\”之后的任何字符的模式。注意,因为'\'是转义字符,所以它前面必须有另一个'\'。
如果你想让它只接受字母表中的字符,你可以使用“-/^[A-z]+$/”,它基本上只查找直接跟在字母表中的字符的连字符。
我建议您仔细阅读正则表达式,以及它提供的不同表达式。
此外,学习正则表达式的一个很好的资源是 Regexr.com,它允许您查找正则表达式参考,并在线尝试各种表达式。