溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

在Java中正則表達(dá)式匹配不到結(jié)果如何解決

發(fā)布時(shí)間:2021-02-23 14:44:54 來源:億速云 閱讀:729 作者:戴恩恩 欄目:開發(fā)技術(shù)

這篇文章主要介紹了在Java中正則表達(dá)式匹配不到結(jié)果如何解決,億速云小編覺得不錯(cuò),現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨億速云小編來看看吧!

Java是什么

Java是一門面向?qū)ο缶幊陶Z言,可以編寫桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序。

如下所示:

String str = "\uFEFF<?xml version=\"1.0\" encoding=\"utf-8\"?><Response xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><Header ShouldRecordPerformanceTime=\"false\" Timestamp=\"2018-6-25 21:24:03\" RequestID=\"2c4d0b24-fd48-4a92-a2d8-c66793df2059\" ResultCode=\"Success\" AssemblyVersion=\"2.9.5.0\" RequestBodySize=\"0\" SerializeMode=\"Xml\" RouteStep=\"1\" Environment=\"pro\" /><SSPATResponse><Result>0</Result><FareDetail /><Price>0</Price><ErrCode>102</ErrCode><DetailInfo>Send:APPLOCK\n" +
    "Rev:\n" +
    "可用資源鎖定成功, 60 秒內(nèi)沒有輸入指令資源將被Buk收回\n" +
    "Send:IG\n" +
    "Rev:\n" +
    "NO PNR\n" +
    "Send:\n" +
    "SS:AA186/N/27JUN18/PEKORD/NN1;\n" +
    "Rev:\n" +
    "AA 186 N 27JUN PEKORD NN1 WL OPEN \n" +
    "UNABLE TO SELL.PLEASE CHECK THE AVAILABILITY WITH \"AV\" AGAIN\n" +
    "Send:IG\n" +
    "Rev:</DetailInfo><PatOfficeno>SHA717</PatOfficeno></SSPATResponse><ResponseStatus><Timestamp xmlns=\"http://soa.ctrip.com/common/types/v1\">2018-06-25T21:24:03.4535624+08:00</Timestamp><Ack xmlns=\"http://soa.ctrip.com/common/types/v1\">Success</Ack></ResponseStatus></Response>";
 
String regex = "<DetailInfo>((.|\\n")*?)</DetailInfo>";

str為要匹配的字符串(是傳入的),regex為正則表達(dá)式

目的是匹配出<DetailInfo>標(biāo)簽中的內(nèi)容

在本地測(cè)試時(shí)可以匹配出來,但是在線上就不行。

真的是百思不得其解……

后來認(rèn)真比對(duì)了一下線上傳入的str和本地復(fù)制過來的str,發(fā)現(xiàn)了了一個(gè)微小的不同

線上傳入的str行分隔符是\r\n,而復(fù)制粘貼到本地之后都變成了\n

而我的正則表達(dá)式中只匹配了\n的情況,因此出現(xiàn)這樣的bug

提醒自己要注意系統(tǒng)之間的差別,win上的行分隔符是\n,而Linux是\r\n

為了能適配所有的環(huán)境,可以直接用System.lineSeparator()來替代,當(dāng)然也可以把表達(dá)式寫成這樣(

<DetailInfo>((.|\\n|\\r\\n")*?)</DetailInfo>

補(bǔ)充:Java正則表達(dá)式匹配的坑

今天在判斷字符串是否存在某個(gè)字符串,直接用String.matches(regex),死活匹配不出來,在線正則工具用了很多都是可以的,后面找到問題,總結(jié)一下,防止再次踩坑。

一、前提#

java中判斷一段字符串中是否包含某個(gè)字符串的方式:

1、#

String.matches(regex);

閱讀源碼發(fā)現(xiàn),這個(gè)方法本質(zhì)是調(diào)用了Pattern.matches(regex, str),而該方法調(diào)Pattern.compile(regex).matcher(input).matches()方法,而Matcher.matches()方法試圖將整個(gè)區(qū)域與模式匹配,如果匹配成功,則可以通過開始、結(jié)束和組方法獲得更多信息。

即這個(gè)方法會(huì)在表達(dá)式前后加上$(regex$),是對(duì)這個(gè)字符串全匹配

而不會(huì)只匹配其中的子串,如果只想匹配子串,則需要表達(dá)式匹配整段

2、#

Pattern.compile(regex).matcher(str).find()

Matcher.find()方法則是僅僅進(jìn)行匹配字串的方法

如果不想使用全局匹配則可以使用Matcher.find()方法

二、附源碼#

1、String.matches(regex)#

String.matches(regex)

public boolean matches(String regex) {
    return Pattern.matches(regex, this);
}
Pattern.matches(regex, this)
public static boolean matches(String regex, CharSequence input) {
  Pattern p = Pattern.compile(regex);
  Matcher m = p.matcher(input);
  return m.matches();
}

2、Matcher.find()#

Pattern.compile

public static Pattern compile(String regex) {
    return new Pattern(regex, 0);
}
Pattern.matcher
public Matcher matcher(CharSequence input) {
    if (!compiled) {
      synchronized(this) {
        if (!compiled)
          compile();
      }
    }
    Matcher m = new Matcher(this, input);
    return m;
}

Matcher.find()

public boolean find() {
    int nextSearchIndex = last;
    if (nextSearchIndex == first)
      nextSearchIndex++;
    // If next search starts before region, start it at region
    if (nextSearchIndex < from)
      nextSearchIndex = from;
    // If next search starts beyond region then it fails
    if (nextSearchIndex > to) {
      for (int i = 0; i < groups.length; i++)
        groups[i] = -1;
      return false;
    }
    return search(nextSearchIndex);
}

以上就是億速云小編為大家收集整理的在Java中正則表達(dá)式匹配不到結(jié)果如何解決,如何覺得億速云網(wǎng)站的內(nèi)容還不錯(cuò),歡迎將億速云網(wǎng)站推薦給身邊好友。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI