检测到符号后删除单词

发布于 2024-11-09 14:43:11 字数 550 浏览 0 评论 0原文

如何从文本文件中删除前面带有符号的单词?

例如:

This is important information...  //but this is a comment
This is more important info...  //and this is another comment

如何删除带有符号“//but this is a comment”的单词?

这是我的伪代码:

1. If "//" is detected, line.replace "//" symbol
2. Clear the words after the symbol 
3. Go on to the next line till you see "//" symbol
4. Repeat steps 1-3 (loop).

注意:这是在读取文件时发生的:

String line;
while ((line = textReader.readLine()) != null) 

How do I remove words from a text file that have symbols preceding them?

For example:

This is important information...  //but this is a comment
This is more important info...  //and this is another comment

How do I remove the words along with the symbol "//but this is a comment" ?

Here's my pseudocode:

1. If "//" is detected, line.replace "//" symbol
2. Clear the words after the symbol 
3. Go on to the next line till you see "//" symbol
4. Repeat steps 1-3 (loop).

Note: this is occurring while the file is being read:

String line;
while ((line = textReader.readLine()) != null) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

零時差 2024-11-16 14:43:11

我假设给定:

This is important information...  //but this is a comment
This is more important info...  //and this is another comment

你想要:

This is important information...
This is more important info...

像这样的东西应该有效:

Pattern pattern = Pattern.compile("//.*$", Pattern.DOTALL);
Matcher matcher = pattern.matcher(line);

line = matcher.replaceFirst("");

Pattern 是 Java 用于正则表达式的内容。以下是关于 Java 中的 Java 正则表达式的一些信息。我使用的正则表达式查找两个正斜杠以及之后的所有内容,直到行尾。然后,匹配的文本将替换为空字符串。 Pattern.DOTALL 告诉 Java 将 ^$ 视为行首和行尾标记。

编辑

下面的代码演示了它的工作原理:

import java.util.regex.*; 

public class RemoveComments { 

   public static void main(String[] args){ 

      String[] lines = {"This is important information...  //but this is a comment", "This is more important info...  //and this is another comment"}; 
      Pattern pattern = Pattern.compile("//.*$", Pattern.DOTALL); 

      for(String line : lines) { 
          Matcher matcher = pattern.matcher(line); 

          System.out.println("Original: " + line); 
          line = matcher.replaceFirst(""); 

          System.out.println("New: " + line); 
      } 
   } 
}

I'm assuming that given:

This is important information...  //but this is a comment
This is more important info...  //and this is another comment

You want:

This is important information...
This is more important info...

Something like this should work:

Pattern pattern = Pattern.compile("//.*$", Pattern.DOTALL);
Matcher matcher = pattern.matcher(line);

line = matcher.replaceFirst("");

Pattern is what Java uses for regular expressions. Here's some information about Java regular expressions in Java. The regex I've used looks for two forward slashes and everything after that until the end of the line. Then, the text that is matched is replaced by an empty string. Pattern.DOTALL tells Java to treat ^ and $ as beginning and end-of-line markers.

EDIT

This code below demonstrates how it works:

import java.util.regex.*; 

public class RemoveComments { 

   public static void main(String[] args){ 

      String[] lines = {"This is important information...  //but this is a comment", "This is more important info...  //and this is another comment"}; 
      Pattern pattern = Pattern.compile("//.*$", Pattern.DOTALL); 

      for(String line : lines) { 
          Matcher matcher = pattern.matcher(line); 

          System.out.println("Original: " + line); 
          line = matcher.replaceFirst(""); 

          System.out.println("New: " + line); 
      } 
   } 
}
回眸一笑 2024-11-16 14:43:11

只是抛出一个想法,您可以使用 String 的功能

,首先找到删除的字符

int i = indexOf('//', 0);

然后查找下一个空格的索引

secondIndex = indexOf(' ',i);

,然后您可以提取两侧

String s1 = subString(0,i);

String s2 = subString(secondIndex,i);

String res = s1+s2;

这不是最佳选择,但应该完成工作^^

Just throwing an idea, you could play around with the functions of String

first locate the charaters that removes

int i = indexOf('//', 0);

Then look for the index of the next space

secondIndex = indexOf(' ',i);

then you can extract both side

String s1 = subString(0,i);

String s2 = subString(secondIndex,i);

String res = s1+s2;

This is not optimal but should get the job done ^^

情何以堪。 2024-11-16 14:43:11

您可以使用 String.replaceAll() 在一行中进行正则表达式替换:

line = line.replaceAll("//.*$", "");

You could use String.replaceAll() to do a regular expression replacement in one line:

line = line.replaceAll("//.*$", "");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文