简体   繁体   English

如何使用Java正则表达式解决此问题?

[英]how can I fix this result with java regex?

I have a line like this : 我有这样一条线:

EQ=ENABLED,QLPUB=50,EPRE=ENABLED,T200=44-31-41-90-90-90-135

with java Regex ,I want to show this : 使用Java Regex,我想显示一下:

EQ=ENABLED,QLPUB=50,EPRE=ENABLED

I wrote this as Regex : 我把它写为Regex:

^EQ=ENABLED,QLPUB=[^,]*,EPRE=ENABLED$

but it's not show me correctly , why? 但这没有正确显示给我,为什么? thanks 谢谢

Thanks for your help ... 谢谢你的帮助 ...

The $ at the end means it will only match the end of the string. 末尾的$表示它将仅匹配字符串的末尾。 You just want to stop the match at the end, not require that it is the end of the input. 你只是想在年底停止比赛,不要求输入的结束。 Try just: 尝试一下:

^EQ=ENABLED,QLPUB=[^,]*,EPRE=ENABLED

Sample code: 样例代码:

import java.util.regex.*;

public class Test
{
    public static void main(String[] args)
    {
        String text = "EQ=ENABLED,QLPUB=50,EPRE=ENABLED,T200=44-31-41-90-90-90-135";
        Pattern pattern = Pattern.compile("^EQ=ENABLED,QLPUB=[^,]*,EPRE=ENABLED");
        Matcher matcher = pattern.matcher(text);
        if (matcher.lookingAt())
        {
            System.out.println(matcher.group());
        }
    }
}

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

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