简体   繁体   English

如何在 android 中使用正则表达式替换字符串中的某些部分,除了某些单词

[英]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.

相关问题 如何用另一个表达式(字符串)替换字符串中的特定单词,另一个表达式必须获取特定单词的某些部分 - How can i replace particular word from a string with another expression(string) and the another expression has to get some part of particular word 如何在Java中字符串的某些部分中使用正则表达式? - How to use regular expression in some part of string in java? 如何使用Java中的正则表达式用字符串中的连续数字替换某些子字符串? - How to replace some substrings with consequitve numbers in a string using regular expression in java? 用正则表达式替换字符串的一部分 - Replace part of the String by Regular Expression 如何在嵌套字符串中使用正则表达式替换? - How to replace using regular expression in nested string? 正则表达式替换但保留部分字符串 - Regular expression replace but keep part of the string 正则表达式以字符串开头的字符串 - Regular expression for a string starting with some string 正则表达式替换字符串中的单词 - Regular Expression to replace word from String ANTLR4 如何创建一个正则表达式,允许除这两个语句之外的所有语句//如何删除输入的一部分 - ANTLR4 How can I create a regular expression that allows all except this two statements // How to delete a part of a input 如何使用正则表达式捕获字符串的一部分? - how can I capture part of a string using regular expressions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM