简体   繁体   English

如何获取单词和等号以及等号和值之间的文本?

[英]How to get the text between a word and an equals sign and an equals sign and a value?

I am learning regular expressions in Java (technically the Android API) and have come across a problem.我正在学习 Java 中的正则表达式(技术上是 Android API)并且遇到了一个问题。 I was originally using basic functionality of Java's String.matchAll(String expression, String occurence);我最初使用的是 Java 的String.matchAll(String expression, String occurence);基本功能String.matchAll(String expression, String occurence); but now I use Pattern and Matcher classes.但现在我使用 Pattern 和 Matcher 类。

I followed this tutorial to learn the basics and it seemed pretty simple.我按照本教程学习了基础知识,看起来很简单。

My Java regex is info(.*?)\\\\, and I have also tried this: (?<=\\\\binfo\\\\=).*?(?=\\\\=) .我的 Java 正则表达式是info(.*?)\\\\,我也试过这个: (?<=\\\\binfo\\\\=).*?(?=\\\\=)

For the first regex if I had a string "info i = 5," it would parse as "info = 5" .对于第一个正则表达式,如果我有一个字符串"info i = 5,"它会解析为"info = 5" For the second one, if using the same string, I would get an exeption that there are no matches (InputMismatchException, I think).对于第二个,如果使用相同的字符串,我会得到一个没有匹配项的例外(InputMismatchException,我认为)。

My parsing code is:我的解析代码是:

//Produces "info i 5" rather than the desired "i"
public String parseStringAlias(String textToBeParsed)
{
    //Gets the value(or alias) located between the word info and the = sign
    Pattern p = Pattern.compile("info(.*?)\\=");
    Matcher m = p.matcher(textToBeParsed);

    //0 would be the first match
    return m.group(0);
}

//Returns InputMismatchException rather than the desired number between equals sign and comma
//If given out example of "info i = 5," should return 5
public String parseStringValue(String textToBeParsed)
{
    //Pattern fetches the value between the "=" and the ","
    Pattern p = Pattern.compile("(?<==).*?(?=\\,)");

    //Search for matches
    Matcher m = p.matcher(textToBeParsed);

    //0 would be the first match
    return m.group(0);
}

You have some issues with your regexps: you should escape only special regex characters like ( , ) , [ , etc. with \\\\ , not with // and not such chracters as , or = .你的正则表达式有一些问题:你应该只使用\\\\转义特殊的正则表达式字符,如( , )[等,而不是使用//而不是像,=这样的字符。

You do not match anything because you do not "run" the regex search.您没有匹配任何内容,因为您没有“运行”正则表达式搜索。

Add m.find() before each return line, better, use it inside an if .在每个返回行之前添加m.find() ,最好在if使用它。 And it looks like you are seeking .group(1) value with parseStringAlias method:看起来您正在使用parseStringAlias方法寻找.group(1)值:

public static void main (String[] args) throws java.lang.Exception
{
    System.out.println(parseStringAlias("\"info i = 5,\""));
    System.out.println(parseStringValue("\"info i = 5,\""));
}
//Produces "info i 5" rather than the desired "i"
public static String parseStringAlias(String textToBeParsed)
{
    //Gets the value(or alias) located between the word info and the = sign
    Pattern p = Pattern.compile("info(.*?)="); // <- Note removed `//`
    Matcher m = p.matcher(textToBeParsed);
    //0 would be the first match
    if (m.find()) {                          // < - We "ran" the regex search
        return m.group(1);                   // <- Group 1 is accessed
    }
    return "";
}   

//Returns ImputMismatchException rather than the desired number between equals sign and comma
//If given out example of "info i = 5," should return 5
public static String parseStringValue(String textToBeParsed)
{
    //Pattern fetches the value between the "=" and the ","
    Pattern p = Pattern.compile("(?<==).*?(?=,)");          // <- Note removed `\\` and `//`
    //Search for matches
    Matcher m = p.matcher(textToBeParsed);
    //0 would be the first match
    if (m.find()) {                               // <- We "ran" the regex
        return m.group(0);                       // <- We access 0th group, the whole match
    }
    return "";
}

See IDEONE demoIDEONE 演示

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

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