Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在单词和等号、等号和值之间获得文本?_Java_Android_Regex_String - Fatal编程技术网

Java 如何在单词和等号、等号和值之间获得文本?

Java 如何在单词和等号、等号和值之间获得文本?,java,android,regex,string,Java,Android,Regex,String,我正在学习Java正则表达式(从技术上讲是Android API),遇到了一个问题。我最初使用Java的基本功能,但现在我使用模式和匹配器类 我跟着学基础知识,看起来很简单 我的Java正则表达式是info(.*?\ \,,我也试过:(?你的正则表达式有一些问题:你应该只转义特殊的正则表达式字符,比如(,),,[,等等。使用\,而不是//code>,或者= 您没有匹配任何内容,因为您没有“运行”正则表达式搜索 在每个返回行之前添加m.find(),最好在if中使用它。它看起来像是在查找。使用pa

我正在学习Java正则表达式(从技术上讲是Android API),遇到了一个问题。我最初使用Java的基本功能,但现在我使用模式和匹配器类

我跟着学基础知识,看起来很简单


我的Java正则表达式是
info(.*?\ \,
,我也试过:
(?你的正则表达式有一些问题:你应该只转义特殊的正则表达式字符,比如
[
,等等。使用
\
,而不是
//code>,或者
=

您没有匹配任何内容,因为您没有“运行”正则表达式搜索

在每个返回行之前添加
m.find()
,最好在
if
中使用它。它看起来像是在查找
。使用
parseStringAlias
方法组(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 "";
}
publicstaticvoidmain(字符串[]args)抛出java.lang.Exception
{
System.out.println(parseStringAlias(“\”信息i=5,\”);
System.out.println(parseStringValue(“\”信息i=5,\”);
}
//生成“信息i 5”而不是所需的“i”
公共静态字符串parseStringAlias(字符串TextToParsed)
{
//获取位于单词info和=符号之间的值(或别名)

Pattern p=Pattern.compile(“info(.*?”);//您没有匹配任何内容,因为您没有运行正则表达式搜索。在返回行之前添加
m.find()
。看起来您正在查找
。组(1)
值。在您指出代码中的//不正确后,我才注意到。谢谢。因为我是从iPad上键入的,所以输入很简单(我在电脑上编码,但一天中的大部分时间我都无法访问我的台式机或笔记本电脑),我的原始代码在这方面是正确的。我将在我的帖子中修复转义。我根据逻辑在“+”和“-”以及
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 "";
}