简体   繁体   English

在java中将正则表达式模式替换为小写

[英]Replace regex pattern to lowercase in java

I'm trying to replace a url string to lowercase but wanted to keep the certain pattern string as it is.我正在尝试将 url 字符串替换为小写,但希望保持特定模式字符串的原样。

eg: for input like:例如:对于输入,如:

http://BLABLABLA?qUERY=sth&macro1=${MACRO_STR1}&macro2=${macro_str2}

The expected output would be lowercased url but the multiple macros are original:预期的输出将是小写的 url 但多个宏是原始的:

http://blablabla?query=sth&macro1=${MACRO_STR1}&macro2=${macro_str2}

I was trying to capture the strings using regex but didn't figure out a proper way to do the replacement.我试图使用正则表达式捕获字符串,但没有找到进行替换的正确方法。 Also it seemed using replaceAll() doesn't do the job.此外,似乎使用 replaceAll() 不能完成这项工作。 Any hint please?请问有什么提示吗?

It looks like you want to change any uppercase character which is not inside ${...} to its lowercase form. 看起来您想将${...}内的所有大写字符更改为小写形式。

With construct 与构造

Matcher matcher = ...

StringBuffer buffer = new StringBuffer();
while (matcher.find()){
    String matchedPart = ...
    ...
    matcher.appendReplacement(buffer, replacement); 
}
matcher.appendTail(buffer);
String result = buffer.toString();

or since Java 9 we can use Matcher#replaceAll​(Function<MatchResult,String> replacer) and rewrite it like 或者从Java 9开始,我们可以使用Matcher#replaceAll​(Function<MatchResult,String> replacer)并将其重写为

String replaced = matcher.replaceAll(m -> {
    String matchedPart = m.group();
    ...
    return replacement;
});

you can dynamically build replacement based on matchedPart . 您可以基于matchedPart动态构建replacement

So you can let your regex first try to match ${...} and later (when ${..} will not be matched because regex cursor will not be placed before it) let it match [AZ] . 因此,您可以让您的正则表达式首先尝试匹配${...} ,然后再进行匹配(当${..}不匹配时,因为不会在其之前放置正则表达式光标),让它匹配[AZ] While iterating over matches you can decide based on match result (like its length or if it starts with $ ) if you want to use use as replacement its lowercase form or original form. 在遍历匹配项时,如果要使用use替代其小写形式或原始形式,则可以根据匹配结果(例如其长度或是否以$开头)进行决定。

BTW regex engine allows us to place in replacement part $x (where x is group id) or ${name} (where name is named group) so we could reuse those parts of match. BTW正则表达式引擎允许我们将replacement零件$x (其中x是组ID)或${name} (其中name称为group)放置在replacement零件中,以便我们可以重复使用那些匹配的零件。 But if we want to place ${..} as literal in replacement we need to escape \\$ . 但是,如果我们想将${..}作为文字替换,则需要转义\\$ To not do it manually we can use Matcher.quoteReplacement . Matcher.quoteReplacement手动进行操作,我们可以使用Matcher.quoteReplacement

Demo: 演示:

String yourUrlString = "http://BLABLABLA?qUERY=sth&macro1=${MACRO_STR1}&macro2=${macro_str2}";

Pattern p = Pattern.compile("\\$\\{[^}]+\\}|[A-Z]");
Matcher m = p.matcher(yourUrlString);

StringBuffer sb = new StringBuffer();
while(m.find()){
    String match = m.group();
    if (match.length() == 1){
        m.appendReplacement(sb, match.toLowerCase());
    } else {
        m.appendReplacement(sb, Matcher.quoteReplacement(match));
    }
}
m.appendTail(sb);
String replaced = sb.toString();
System.out.println(replaced);

or in Java 9 或在Java 9中

String replaced = Pattern.compile("\\$\\{[^}]+\\}|[A-Z]")
        .matcher(yourUrlString)
        .replaceAll(m -> {
            String match = m.group();
            if (match.length() == 1)
                return match.toLowerCase();
            else
                return Matcher.quoteReplacement(match); 
        });
System.out.println(replaced);

Output: http://blablabla?query=sth&macro1=${MACRO_STR1}&macro2=${macro_str2} 输出: http://blablabla?query=sth&macro1=${MACRO_STR1}&macro2=${macro_str2}

This regex will match all the characters before the first &macro , and put everything between http:// and the first &macro in its own group so you can modify it. 此正则表达式将匹配第一个&macro之前的所有字符,并将所有内容放在http://和第一个&macro ,以便将其修改。

http://(.*?)&macro

Tested here 在这里测试

UPDATE: If you don't want to use groups, this regex will match only the characters between http:// and the first &macro 更新:如果您不想使用组,则此正则表达式将仅匹配http://和第一个&macro之间的字符

(?<=http://)(.*?)(?=&macro)

Tested here 在这里测试

As simple this need some coding . 一样简单,这需要一些编码。 split whole url into parts till marcos and tolower the string 1 variable and append all other remaing macros string to it. 将整个url分成几部分,直到marcos并降低字符串1变量,并将所有其他restg宏字符串附加到该变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM