正则表达式查找字符串中被%包围的变量 [英] regex to find variables surrounded by % in a string

查看:315
本文介绍了正则表达式查找字符串中被%包围的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要在字符串中查找变量".表示变量为%[/w] +%,要注意的是,字符串中可以有多个变量:

Need to find "variables" within a string. What denotes a variable is %[/w]+%, the catch is there can be more than one variable within the string:

%ABC%
%ABC%-%RED%
Lorem ipsum %GeT% sit amet, %% consectetur %QW23% elit. 

在第三个示例中,不应找到%%,它将被单个%代替.像#[\ w +-] +#之类的东西不起作用,因为它无法确定在第二行中它是%ABC%和%RED%,而是%-%.我的印象是需要同时使用组和反向引用,但是我找不到任何很好的示例来说明如何在Java中执行此操作.

In the third example the %% should NOT be found, it will be replaced with a single %. Something like #[\w+-]+# does not work because it cannot determine that in the second line it is %ABC% and %RED%, but rather %-%. I am under the impression that both groups and back references needs to be used but I cannot find any good example to explain how to do this in Java.

人们正在问一些问题的答案,因此,您可以开始:

Folks are asking for some answers to questions, so here you go:

我期望最终输出的确切内容是什么?好吧,正如受试者所暗示的那样,%ABC%是在其他地方定义的变量",最终目标是找到变量并将其替换为正确的值".正则表达式的目标是找到字符串中的所有变量".

What exactly am I expecting as the final output? Well, as the subject suggests the %ABC% is a 'variable' that is defined somewhere else, the final goal is to "find the variable and replace it with the correct value". The goal with the regular expression is to find all the 'variables' within a string.

因此,内存中的某个位置有一个映射:

So, there is a map somewhere in memory where:

ABC = "mike"
RED = "Red Storm"
GeT = "hometown"
QW23 = "Quick and easy"

(旁注:如果键名必须在名称周围加上%,也可以)

(side note: if the keys need to have % around the name, that is OK, too)

正则表达式的目标是查找"变量,因此在第一个字符串中它将找到ABC(或%ABC%),以便代码并查找ABC以确定正确的值是mike,依此类推上...这是给定的字符串的期望输出:

The goal of the regex is to 'find' the variable, so in the first string it will find ABC (or %ABC%) so that the code and look up ABC to determine that the correct value is mike, and so on... Here is the desired output of the strings given:

mike
mike-Red Storm
Lorem ipsum hometown sit amet, % consectetur Quick and easy elit. 

我不希望reg表达式实际执行完全替换,而只是查找片段以便其他代码进行替换.我也不希望它会将%%转换为%,而是将其保留下来,这样一来,简单地搜索%%就可以将其转换为%.

I am not expecting the reg expression to actually do the full replace, but simply to find the pieces so that other code and do the replace. I am also not expecting it to convert the %% to the %, but leave it alone so that after the fact a simple search for %% can convert it to %.

推荐答案

我相信您正在寻找正则表达式模式

I believe you are looking for a regex pattern

(?<!%%)(?<=%)\w+(?=%)(?!%%)

这将找到在每边用单个%字符包围的变量.

That would find variables that are surrounded by a single % character on each side.

此处 中测试正则表达式.

Test the regex here.

Java代码:

final Pattern pattern = Pattern.compile("(?<!%%)(?<=%)\\w+(?=%)(?!%%)");
final Matcher matcher = pattern.matcher(input);

while (matcher.find()) {
    System.out.println(matcher.group(0));
}

测试Java代码此处 .

Test the Java code here.

如果您要按照下面的注释中的要求捕获组,请使用以下模式:

If you want to catch groups as requested in your comment below, use the following pattern:

(?<!%)(%)(\w+)(%)(?!%)

此处 进行测试.

Test this pattern here.

...和Java代码:

...and Java code:

final Pattern pattern = Pattern.compile("(?<!%)(%)(\\w+)(%)(?!%)");
final Matcher matcher = pattern.matcher(input);

while (matcher.find()) {
    System.out.println(matcher.group(1) + " | " + 
                       matcher.group(2) + " | " + 
                       matcher.group(3));
}    

此处 中测试此代码.

Test this code here.

这篇关于正则表达式查找字符串中被%包围的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆