2013-02-15 42 views
1

Java新手在這裏。Java:正則表達式來解析字符串的「邊緣」

說我是給定一個字符串:

===這銳是= STRI = NG身===

我將如何使用模式匹配有效地找出在「這個銳利是= stri = ng身」的邊緣有多少「=」號?

此外,我試圖使用Java轉義序列,如\ G,但顯然他們不編譯。

+0

長度你問的正則表達式做匹配或一種方式來獲得計數? – Thihara 2013-02-15 05:02:34

+1

答案是3.根據[文檔],'\ G'在Java中工作得很好(http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/ Pattern.html)。 – Qtax 2013-02-15 05:04:06

+2

爲什麼你會使用正則表達式呢? – 2013-02-15 05:14:24

回答

3

我個人可能不會使用這個正則表達式,但...這是什麼工作:

Matcher m = Pattern.compile("^(=+).+[^=](=+)$").matcher("===Som=e=Text===="); 
m.find(); 
int count = m.group(1).length() + m.group(2).length(); 
System.out.println(count); 

(請注意,這不是做錯誤檢查,並假設有=兩端)

編輯添加的:而這裏的一個工程,無論是否有=在兩端:

public static int equalsCount(String source) 
{ 
    int count = 0; 
    Matcher m = Pattern.compile("^(=+)?.+[^=](=+)?$").matcher(source); 
    if (m.find()) 
    { 
     count += m.group(1) == null ? 0 : m.group(1).length(); 
     count += m.group(2) == null ? 0 : m.group(2).length(); 
    } 

    return count; 
} 

public static void main(String[] args) 
{ 
    System.out.println(equalsCount("===Some=tex=t=")); 
    System.out.println(equalsCount("===Some=tex=t")); 
    System.out.println(equalsCount("Some=tex=t=")); 
    System.out.println(equalsCount("Some=tex=t")); 
} 

在另一方面...你可以避開正則表達式,並做到:

String myString = "==blah="; 
int count = 0; 
int i = 0; 
while (myString.charAt(i++) == '=') 
{ 
    count++;  
} 
i = myString.length() - 1; 
while (myString.charAt(i--) == '=') 
{ 
    count++; 
} 
+0

不知道爲什麼我沒有想到這......我就是這麼做的... C必須是 – Soyuz 2013-02-15 06:31:16

+0

我的背景是C語言;我是一個糟糕的Java程序員......總是希望完成任務而不是使用框架或設計模式;) – 2013-02-15 06:33:42

+0

對於給定的問題,使用Pattern.compile(「^(= +)。+(= +)$」)就足夠了。使用[^ =]有什麼特別的理由? – MGPJ 2013-03-08 02:30:52

1

如果要計算邊緣出現「=」的次數,請嘗試此操作。

int count = str.length() - str.replaceAll("[^=]=[^=]", "").length(); 
+0

你錯過了問題的重要部分,*「在邊緣」*。提示:添加一些錨點,量詞和變化。 – Qtax 2013-02-15 05:06:09

+1

這將返回所有等號的數量,而不僅僅是邊界「這是一個= stri = ng身」的數字。 – Dracs 2013-02-15 05:07:06

+0

它也會刪除正文中的等號。雖然我不確定它是否會全部獲得。''[^ =] +(= [^ =])*'應該更好用 – Cephalopod 2013-02-15 05:30:22

0

這可能是一個可能的答案:

public static void main(String[] args) { 
    int count = 0; 
    String str = "===This is a= stri = ng==="; 
    Pattern edgeEq = Pattern.compile("="); 
    Pattern wordEq = Pattern.compile("[^=]=+[^=]"); 

    Matcher edgeMatch = edgeEq.matcher(str); 
    while (edgeMatch.find()) { 
     count++; 
    } 

    Matcher wordMatch = wordEq.matcher(str); 
    while (wordMatch.find()) { 
     count--; 
    } 

    System.out.println(count); 
} 

這將幫助你找到=在弦上的邊緣號碼。

0

假設總是有相同數量的=在開始作爲結尾:

import java.util.regex.*; 
Matcher m = Pattern.compile("^=*").matcher(s); 
int count = m.find()? m.group(0).length(): 0; 
0

使用下面的代碼

String s1 = "===This 銳is a= stri = ng身==="; 
    System.out.println("Length : "+s1.length()); 
    p = Pattern.compile("^=+"); 

    m = p.matcher(s1); 
    int count = 0; 
    while (m.find()) 
    { 
     count = m.group().length(); 
     System.out.println("Group : "+m.group()); 
    } 


    p = Pattern.compile("(=+)$"); 
    m = p.matcher(s1); 
    while (m.find()) 
    { 
     count += m.group().length(); 
     System.out.println("End Group : "+m.group()); 
    } 

    System.out.println("Total : " + count); 
0

如果=在邊緣處是平衡的,你可以使用

^(=+).*\1$ 

組別1的長度是=在邊緣

相關問題