检测到符号后删除单词
如何从文本文件中删除前面带有符号的单词?
例如:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设给定:
你想要:
像这样的东西应该有效:
Pattern
是 Java 用于正则表达式的内容。以下是关于 Java 中的 Java 正则表达式的一些信息。我使用的正则表达式查找两个正斜杠以及之后的所有内容,直到行尾。然后,匹配的文本将替换为空字符串。Pattern.DOTALL
告诉 Java 将^
和$
视为行首和行尾标记。编辑
下面的代码演示了它的工作原理:
I'm assuming that given:
You want:
Something like this should work:
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:
只是抛出一个想法,您可以使用 String 的功能
,首先找到删除的字符
然后查找下一个空格的索引
,然后您可以提取两侧
这不是最佳选择,但应该完成工作^^
Just throwing an idea, you could play around with the functions of String
first locate the charaters that removes
Then look for the index of the next space
then you can extract both side
This is not optimal but should get the job done ^^
您可以使用
String.replaceAll()
在一行中进行正则表达式替换:You could use
String.replaceAll()
to do a regular expression replacement in one line: