使用正则表达式从数据中获取特定字符串

时间:2016-08-01 02:56:01

标签: java regex

我正在尝试从下面的数据中获取特定的字符串。我在这里分享样本数据的时间太长了。从这里我必须得到'france24Id=7GHYUFGty6fdGFHyy56' 与正则表达式相比,我并不那么熟悉。

如何从上述数据中检索字符串'france24Id = 7GHYUFGty6fdGFHyy56'?

我尝试使用','分割数据,但这不是一种有效的方式。这就是我选择正则表达式的原因。

2016-07-29 12:08:46,260 s=xGuide, [xre-10-pipe@6da05f7a[,connection=WebSocketConnectionWrapper[/1.8.9]]] INFO  c=c.ore., - onConnect event payload={minimumVersion='0', applicationName='shell', fetl='555', authenticationToken='6y777', sessionAuthToken='', sessionGUID='null', connectURL='http://section?ruleName=Default', partnerId='hp', nativeDimensions=null, appParams={heartbeatRequest=1, france24Id=7GHYUFGty6fdGFHyy56, service=false, networkBuffer={min=150, max=150, step=0}}, deviceCaps={platform=Mac, receiverType=Native, revisions={protocol=1, auth=1, video=1}, pixelDimensions=[1280, 720]}, forceSource=null, reconnect=false, currentCommandIndex=0, reconnectReason=7, authService=9}

4 个答案:

答案 0 :(得分:3)

您可以使用(france\d+Id)=([a-zA-Z0-9]+),获得所需内容。这将获取您的字符串并将其中的两部分转储到适合平台的捕获组变量中(例如,分别在Perl中,$1$2)。

在Java中,您的代码看起来有点像这样:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public String matchID(String data) {
    Pattern r = new Pattern("(france\\d+Id)=([a-zA-Z0-9]+),");
    Matcher m = r.matcher(data);
    return m.group(2);
}

答案 1 :(得分:0)

    public static void main(String[] args) {
    String str = "2016-07-29 12:08:46,260 s=xGuide, [xre-10-pipe@6da05f7a[,connection=WebSocketConnectionWrapper[/1.8.9]]] INFO  c=c.ore., - onConnect event payload={minimumVersion='0', applicationName='shell', fetl='555', authenticationToken='6y777', sessionAuthToken='', sessionGUID='null', connectURL='http://section?ruleName=Default', partnerId='hp', nativeDimensions=null, appParams={heartbeatRequest=1, france24Id=7GHYUFGty6fdGFHyy56, service=false, networkBuffer={min=150, max=150, step=0}}, deviceCaps={platform=Mac, receiverType=Native, revisions={protocol=1, auth=1, video=1}, pixelDimensions=[1280, 720]}, forceSource=null, reconnect=false, currentCommandIndex=0, reconnectReason=7, authService=9}";
    String regex = ".*(france24Id=[\\d|\\w]*),.*";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(str);
    if(matcher.matches()){
            System.out.println(matcher.group(1));
    }
}

答案 2 :(得分:0)

你也可以尝试这个:

String str = "france24Id=7GHYUFGty6fdGFHyy56";
Pattern pattern = Pattern.compile("(?<=france24Id=)([a-zA-Z0-9]+)");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
    System.out.println("ID = " + matcher.group());
}

结果是:

ID = 7GHYUFGty6fdGFHyy56

答案 3 :(得分:0)

您可以在Java中使用Pattern和Matcher类。

String data = "2016-07-29 12:08:46,260 s=xGuide, [xre-10-pipe@6da05f7a[,connection=WebSocketConnectionWrapper[/1.8.9]]] INFO  c=c.ore., - onConnect event payload={minimumVersion='0', applicationName='shell', fetl='555', authenticationToken='6y777', sessionAuthToken='', sessionGUID='null', connectURL='http://section?ruleName=Default', partnerId='hp', nativeDimensions=null, appParams={heartbeatRequest=1, france24Id=7GHYUFGty6fdGFHyy56, service=false, networkBuffer={min=150, max=150, step=0}}, deviceCaps={platform=Mac, receiverType=Native, revisions={protocol=1, auth=1, video=1}, pixelDimensions=[1280, 720]}, forceSource=null, reconnect=false, currentCommandIndex=0, reconnectReason=7, authService=9}";   
 String regex1 = "france24Id=[a-zA-Z0-9]+"; //this matches france24Id=7GHYUFGty6fdGFHyy56
    String regex2 = "(?<=france24Id=)[a-zA-Z0-9]+"; //this matches 7GHYUFGty6fdGFHyy56 or whatever after "france24Id=" and before ','
    Pattern pattern1 = Pattern.compile(regex1);
    Pattern pattern2 = Pattern.compile(regex2);
    Matcher matcher1 = pattern1.matcher(data);
    Matcher matcher2 = pattern2.matcher(data);

    String result1, result2;

    if(matcher1.find())
        result1 = matcher1.group(); //if match is found, result1 should contain "france24Id=7GHYUFGty6fdGFHyy56"

    if(matcher2.find())
        result2 = matcher2.group(); //if match is found, result1 should contain "7GHYUFGty6fdGFHyy56"