开发者

Repeated regular expression

How can I parse a strings like :

name1="val1"    name2="val2"    name3="val3"

I cannot u开发者_如何学编程se split(\s+) as it can be name = "val 1". I am doing java but any laguage is okay.


Here it is in Java, with a slight variation on the regex, with capturing group to put the name/value pairs into a Map<String,String>.

This usage of Matcher.find() in a while loop is typical.

    import java.util.*;
    import java.util.regex.*;
    //...

    String pattern = "(\\w+)\\s*=\\s*\"([^\"]*+)\"";

    String text = "name1 = \"val 1\"    name2=\"val2\"    name3=\"val3\"";
    System.out.println(text);
    // name1 = "val 1"    name2="val2"    name3="val3"

    Matcher m = Pattern.compile(pattern).matcher(text);
    Map<String,String> map = new HashMap<String,String>();
    while (m.find()) {
        map.put(m.group(1), m.group(2));
    }
    System.out.println(map);
    // {name3=val3, name1=val 1, name2=val2}

API links

  • java.util.Map<K,V>
  • java.util.regex.Pattern
  • java.util.regex.Matcher

regular-expressions.info links

  • Character classes
  • Grouping and backreferences


In Python:

import re
astr='''name1="val 1 "    name2 = "val2"    name3="val3"'''
print(re.findall('\w+\s*=\s*".*?"',astr))
# ['name1="val 1 "', 'name2 = "val2"', 'name3="val3"']
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜