簡體   English   中英

正則表達式幫助從最外面的括號中獲取價值

[英]Regex help to get value from the outer most brackets

您能幫我從最外面的花括號中獲取值嗎,搜索字符串可能會有所不同?

  String search = "hostProductLineCode:eq(YB) || hostProductLineCode:eq(PB) || (readyDate:ge(2014-01-13T05:00:00);StartDate:ge(2014-01-13T05:00:00))";

要從上方獲取以下值YB PB readyDate:ge(2014-01-13T05:00:00); StartDate:ge(2014-01-13T05:00:00)

      search = "(readyDate:ge(2014-01-13T05:00:00);StartDate:ge(2014-01-13T05:00:00)) || (liveDate:ge(2014-01-13T05:00:00);StartDate:ge(2014-01-13T05:00:00))";

想要從上面獲取以下值readyDate:ge(2014-01-13T05:00:00); StartDate:ge(2014-01-13T05:00:00)liveDate:ge(2014-01-13T05:00:00) ;開始日期:ge(2014-01-13T05:00:00)

       search = "hostProductLineCode:eq(YB);hostProductLineCode:eq(PB);(readyDate:ge(2014-01-13T05:00:00)||StartDate:ge(2014-01-13T05:00:00))";

想要從YB上方獲取以下值
PB readyDate:ge(2014-01-13T05:00:00)|| StartDate:ge(2014-01-13T05:00:00)

 search = "hostProductLineCode:eq(YB);hostProductLineCode:eq:eq(V);(readyDate:ge(2014-01-13T05:00:00)||StartDate:ge(2014-01-13T05:00:00));(status:eq(V)||status:eq(P))";

想要從上方獲取以下值

   YB
      V 
     readyDate:ge(2014-01-13T05:00:00)||StartDate:ge(2014-01-13T05:00:00));(status:eq(V)||status:eq(P)

我在下面嘗試過

            Pattern p = Pattern.compile("\\((.*?)\\)");
    Matcher m = p.matcher(search);

    while (m.find()) {
        System.out.println(m.group());
              }

根據||分割字符串 然后使用\\(.*\\)正則表達式進行匹配。 .*是貪婪的量詞。 它試圖獲得最大的匹配。

這是一些代碼。
注意:它是C#,但是您可以根據需要輕松地轉換為Java或JS。 它帶有注釋,因此您可以輕松地遵循邏輯。

它基本上會掃描您的字符串,並在外部括號之間復制所有內容:

void Main()
{
    String search = @"hostProductLineCode:eq(YB) || 
                        hostProductLineCode:eq(PB) || 
                        (readyDate:ge(2014-01-13T05:00:00);StartDate:ge(2014-01-13T05:00:00))";
    GetParenthesis(search).Dump();   

    search = @"(readyDate:ge(2014-01-13T05:00:00);StartDate:ge(2014-01-13T05:00:00)) 
        || (liveDate:ge(2014-01-13T05:00:00);StartDate:ge(2014-01-13T05:00:00))";
    GetParenthesis(search).Dump();      

}

// Define other methods and classes here
private IEnumerable<string> GetParenthesis(string aSource) {
    int status = 0; // you'll copy chars when this is 1 or bigger

    List<string> list_of_string = new List<string>();   // Hold the strings 
    StringBuilder temp_string = new StringBuilder();    // Hold temp string we're copying

    for (int i = 0; i < aSource.Length; i++)
    {
        // Deal with open bracket
        if (aSource[i].Equals('(')) {
            if (status ==0)
                i++; // to skip the first "("
            status++;
        }
        // Deal with close bracket
        else if (aSource[i].Equals(')')) {
            status--;
            if (status==0) { // need to add to list
                list_of_string.Add(temp_string.ToString());
                temp_string.Clear();    // reset temp string
            }
        }

        // Copy if in between brackets
        if (status>=1)
            temp_string.Append(aSource[i]);
    }

    return list_of_string;
}

運行主要部分(我正在使用linqpad),將返回:

List<String> (3 items)
YB 
PB 
readyDate:ge(2014-01-13T05:00:00);StartDate:ge(2014-01-13T05:00:00) 

List<String> (2 items) 
readyDate:ge(2014-01-13T05:00:00);StartDate:ge(2014-01-13T05:00:00) 
liveDate:ge(2014-01-13T05:00:00);StartDate:ge(2014-01-13T05:00:00) 

這似乎是您想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM