开发者

Regex match question

If I have a string such as

lotsofcrap"somethingimportant"moreotherstuff

is it possible to get Regex to match just whatever开发者_如何学Python is between the " ", excluding the quotation marks? So the way to detect it would be something like ".*", but that will return "somethingimportant" rather than just pure somethingimportant


"(.*)"

You can use parenthese to create a capturing group. How you access it depends on the language/library you're using--typically the capture groups are available as $1 or \1 in Perl-like languages. For example, in Perl:

'hello "world" !!!' =~ /"(.*)"/;
print "$1\n";


If your regex engine supports zero-width assertions (look-behinds and look-aheads),

(?<=")[^"]*(?=")

will match a sequence of non-quote characters, where there occurs a quote before and a quote after.

However, this is silly. You should simply

"([^"]*)"

match everything, including the quotes, and then pull group 1 (the set of parentheses) out of the match.


Try "(.*?)"

The ? means that the .* will expand as needed (until it matches the next )" in this case).

Java Code:

static String regex = "\"(.*?)\"";
static Pattern p = Pattern.compile(regex);

public static List<String> getMatches(String inputText) {
    Matcher m = p.matcher(inputText);
    List<String> list = new ArrayList<String>();
    while(m.find()){
        list.add(m.group(1));
    }
    return list;
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜