[英]Java: How to remove strings having the same prefix from a text file?
I need to replace some string from a text file such as "upcoming:event=123982". 我需要替换文本文件中的一些字符串,例如“ upcoming:event = 123982”。
The strings always start with "upcoming:event=". 字符串始终以“ upcoming:event =“开头。 How can I remove them (including the digits) ? 如何删除它们(包括数字)? (I'm processing the text with Java). (我正在用Java处理文本)。
Should I use wildcards ? 我应该使用通配符吗? something like "upcoming:event=*" ? 类似于“ upcoming:event = *”?
thanks 谢谢
String str = "upcoming:event=123982";
System.out.println(str.replaceFirst("upcoming:event=[0-9]*", "changed"));
Output: changed 输出:已更改
String s="upcoming:event=123982;upcoming:event=100;upcoming:event=200;upcoming:event=900;upcoming:event=1987";
System.out.println(s.replaceAll("upcoming:event=[0-9]*", ""));
Output: 输出:
123982;100;200;900;1987
Even though this is probably is not a good way you could 即使这可能不是您可以使用的好方法
String text = "upcoming:event=*YOUR TEXT";
System.out.println(text.replace("upcoming:event=*", ""));
Result will be YOUR TEXT in this case... 在这种情况下,结果将是您的文字...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.