[英]How can i replace some part in string except some word using regular expression in android
I have 100 such files with string containing Vol - {some digit}.我有 100 个这样的文件,其中包含 Vol - {some digit} 的字符串。 I want to replace all word except this Vol - {0-9}.
我想替换除此 Vol - {0-9} 之外的所有单词。
Problem : Kirtan _ Vol - 4 _ android - 1 _ bhai sahib.mp3问题:Kirtan _ Vol - 4 _ android - 1 _ bhai sahib.mp3
Needed Result : Vol - 4.mp3需要的结果:Vol - 4.mp3
You can use the regex , .*(Vol - [0-9]).*
where .*
means any character any number of times and (Vol - [0-9])
is the capturing group (1) referred by $1
in the code.您可以使用正则表达式
.*(Vol - [0-9]).*
其中.*
表示任何字符任意次数, (Vol - [0-9])
是$1
引用的捕获组(1)编码。 The pattern, [0-9]
means one digit in the range, 0-9.模式
[0-9]
表示 0-9 范围内的一位数字。
public class Main {
public static void main(String[] args) {
String str = "Kirtan _ Vol - 4 _ android - 1 _ bhai sahib.mp3";
str = str.replaceAll(".*(Vol - [0-9]).*", "$1");
System.err.println(str);
}
}
Output: Output:
Vol - 4
If the number of digits can be more than one, replace [0-9]
with [0-9]+
or \d+
.如果位数可以多于一位,请将
[0-9]
替换为[0-9]+
或\d+
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.