Java 检测到符号后删除单词

Java 检测到符号后删除单词,java,regex,comments,Java,Regex,Comments,如何从前面有符号的文本文件中删除单词 例如: This is important information... //but this is a comment This is more important info... //and this is another comment 如何删除带有“/”符号的单词,但这是一条注释“ 这是我的伪代码: 1. If "//" is detected, line.replace "//" symbol 2. Clear the words after

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

例如:

This is important information...  //but this is a comment
This is more important info...  //and this is another 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) 

我假设:

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("");
模式
是Java用于正则表达式的模式。下面介绍Java中的Java正则表达式。我使用的正则表达式寻找两个前斜杠和之后的所有东西,直到行尾。然后,匹配的文本将替换为空字符串告诉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); 
      } 
   } 
}

我假设:

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("");
模式
是Java用于正则表达式的模式。下面介绍Java中的Java正则表达式。我使用的正则表达式寻找两个前斜杠和之后的所有东西,直到行尾。然后,匹配的文本将替换为空字符串告诉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); 
      } 
   } 
}

只要提出一个想法,你就可以玩转字符串的功能

首先找到删除的字符

int i = indexOf('//', 0);
然后查找下一个空间的索引

secondIndex = indexOf(' ',i);
然后你可以提取两边

String s1 = subString(0,i);

String s2 = subString(secondIndex,i);

String res = s1+s2;

这不是最优的,但应该完成工作^^

只要提出一个想法,您就可以使用String的函数

首先找到删除的字符

int i = indexOf('//', 0);
然后查找下一个空间的索引

secondIndex = indexOf(' ',i);
然后你可以提取两边

String s1 = subString(0,i);

String s2 = subString(secondIndex,i);

String res = s1+s2;
这不是最优的,但应该可以完成工作^^

您可以使用它在一行中替换正则表达式:

line = line.replaceAll("//.*$", "");
您可以使用在一行中替换正则表达式:

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

模式
是您在Java中用来处理正则表达式的模式。好的,我知道了。我必须导入这些类。@rudna1010是的,
java.util.regex
有这些类。非常好!谢谢我以前从未使用过正则表达式,但现在我知道:)@rudna1010它们很有用,功能强大,但不要被它们的功能所诱惑:)有时它们比它们的价值更麻烦。
Pattern
是Java中处理正则表达式的方法。好的,我明白了。我必须导入这些类。@rudna1010是的,
java.util.regex
有这些类。非常好!谢谢我以前从未使用过正则表达式,但现在我知道:)@rudna1010它们有用且强大,但不要被它们的强大所诱惑:)有时它们比它们的价值更麻烦。阅读Vivin的答案,使用他向你展示的正则表达式可能更简单(对正则表达式没有那么强:P)阅读Vivin的答案,像他向您展示的那样使用regex可能会更简单(regex:P没有那么强)