简体   繁体   English

在以下情况下的正则表达式匹配问题

[英]Regular expression matching issue with the following scenario

I am developing an application. 我正在开发一个应用程序。 User will enter some of the setting value in the server. 用户将在服务器中输入一些设置值。 When I ask for the value to the server through the inbuilt API. 当我通过内置API向服务器请求值时。 I am getting values like as a whole string: 我得到像整个字符串一样的值:
for example- 例如-

name={abc};display={xyz};addressname={123}

Here the properties are name, display and address and there respective values are abc, xyz and 123. 这里的属性是名称,显示和地址,分别是abc,xyz和123。
I used to split with ; 我曾经与; as first delimeter and = as a second dleimeter. 作为第一分度计,=作为第二分度计。

String[] propertyValues=iPropertiesStrings.split(";");
        for(int i=0;i<propertyValues.length;i++)
        {
            if(isNullEmpty(propertyValues[i]))
                continue;

            String[] propertyValue=propertyValues[i].split("=");
            if(propertyValue.length!=2)
                mPropertyValues.put(propertyValue[0], "");
            else
                mPropertyValues.put(propertyValue[0], propertyValue[1]);
        }
    }

here mPropertyValues is hash map which is used for keeping property name and its value. 在这里,mPropertyValues是哈希映射,用于保留属性名称及其值。

Problem is there can be string : 问题是可以有字符串:

case 1:  name={abc};display={ xyz=deno; demo2=pol };addressname={123}
case 2:  name=;display={ xyz=deno; demo2=pol };addressname={123}

I want hashmap to be filled with : 我希望将hashmap填充为:

case 1: 情况1:

name ="abc" 
display = "xyz= demo; demo2 =pol"
addressname = "123"

for case 2: 对于情况2:

name =""
display = "xyz= demo; demo2 =pol"
addressname = "123"

I am looking for a regular expression to split these strings; 我正在寻找一个正则表达式来拆分这些字符串。

以下正则表达式应符合您的条件,并使用命名的捕获组来获取您需要的三个值。

name=\{(?<name>[^}])\};display=\{(?<display>[^}]+)\};addressname=\{(?<address>[^}]\)}

Assuming your dataset can change, a better parser may be more dynamic, building a Map from whatever is found in that return type. 假设您的数据集可以更改,则更好的解析器可能更动态,可以根据该返回类型中找到的内容构建Map

The regex for this is pretty simple, given the cases you list above (and no nesting of {} , as others have mentioned): 考虑到上面列出的情况,此正则表达式非常简单(并且没有嵌套{} ,正如其他人提到的那样):

Matcher m = Pattern.compile("(\\w+)=(?:\\{(.*?)\\})?").matcher(source_string);
while (m.find()) {
    if (m.groupCount() > 1) {
        hashMap.put(m.group(1), m.group(2));
    }
}

There are, however, considerations to this: 但是,有一些注意事项:

  1. If m.group(2) does not exist, "null" will be the value, (you can adjust that to be what you want with a tiny amount of logic). 如果m.group(2)不存在,则将使用“ null”作为值(您可以通过少量逻辑将其调整为所需的值)。
  2. This will account for varying data-sets - in case your data in the future changes. 这将说明变化的数据集-以防将来您的数据发生变化。

What that regex does: 该正则表达式的作用:

  1. (\\\\w+) - This looks for one or more word characters in a row (A-z_) and puts them into a "capture group" ( group(1) ) (\\\\w+) -这将连续查找一个或多个单词字符(A-z_),并将其放入“捕获组”( group(1)
  2. = - The literal equals = -文字等于
  3. (?:...)? - This makes the grouping not a capture group (will not be a .group(n) , and the trailing ? makes it an optional grouping. -这使分组不是捕获组(不会是.group(n).group(n) ?使其成为可选分组。
  4. \\\\{(.*?)\\\\} - This looks for anything between the literals { and } (note: if a stray } is in there, this will break). \\\\{(.*?)\\\\} -这会在文字{}之间寻找任何东西(注意:如果其中有一个杂散的} ,则会中断)。 If this section exists, the contents between {} will be in the second "capture group" ( .group(2) ). 如果存在此部分,则{}之间的内容将在第二个“捕获组”( .group(2) )中。

Assuming that there can't be nested {} this should do what you need 假设不能嵌套{}这应该可以满足您的需求

String data = "name=;display={ xyz=deno; demo2=pol };addressname={123}";

Pattern p = Pattern.compile("(?<name>\\w+)=(\\{(?<value>[^}]*)\\})?(;|$)");
Matcher m = p.matcher(data);

while (m.find()){
    System.out.println(m.group("name")+"->"+(m.group("value")==null?"":m.group("value").trim()));
}

Output: 输出:

name->
display->xyz=deno; demo2=pol
addressname->123

Explanation 说明

(?<name>\\\\w+)=(\\\\{(?<value>[^}]*)\\\\})?(;|$) can be split into parts where (?<name>\\\\w+)=(\\\\{(?<value>[^}]*)\\\\})?(;|$)可分为以下部分:

  • (?<name>\\\\w+)= represents XXXX= and place XXXX in group named name (of property) (?<name>\\\\w+)=代表XXXX =并将XXXX放在名为(属性的name )组中
  • (\\\\{(?<value>[^}]*)\\\\})? is optional {XXXX} part where X can't be } . 是可选的{XXXX}部分,其中X不能是} Also it will place XXXX part in group named value . 同样,它将XXXX部分放置在名为value组中。
  • (;|$) represents ; (;|$)代表; OR end of data (represented by $ anchor) since formula is name=value; OR数据结尾(由$锚表示),因为公式为name=value; or in case of pair placed at the end of data name=value . 或将对放在数据name=value的末尾。

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

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