2017-10-11 143 views
0

我在這個論壇上搜索了很多帖子,令我驚訝的是,我還沒有發現任何人有像我這樣的問題。 我必須爲控制檯的字符串值做一個簡單的計算器。現在,我試圖做一些正則表達式來驗證輸入。 我的計算器必須接受操作符之間有空格的數字(只允許+和 - 是允許的),但不包括數字之間有空格的數字,總結如下: 2 + 2 = 4是正確的,但是 2 2 + 2 - >這應該會產生錯誤,並通知控制檯上的用戶他在數字之間放置了空格。正則表達式匹配數字和運算符之間的空格,但數字之間沒有空格

我想出這個:

static String properExpression = "([0-9]+[+-]?)*[0-9]+$"; 
static String noInput = ""; 
static String numbersFollowedBySpace = "[0-9]+[\\s]+[0-9]"; 
static String numbersWithSpaces = "\\d+[+-]\\d+"; 

//I've tried also "[\\d\\s+\\d]"; 

void validateUserInput() { 
Scanner sc = new Scanner(System.in); 
System.out.println("Enter a calculation."); 
input = sc.nextLine(); 

if(input.matches(properExpression)) { 
calculator.calculate(); 

} else if(input.matches(noInput)) { 
    System.out.print(0); 

} else if(input.matches(numbersFollowedBySpace)) { 
    input.replaceAll(" ", ""); 
    calculator.calculate(); 

    } else if(input.matches(numbersWithSpaces)) 
     { 
      System.out.println("Check the numbers. 
It seems that there is a space between the digits"); 
     } 

    else System.out.println("sth else"); 

你能不能給我一個大概的正則表達式我應該用暗示?

+2

你沒有找到答案的原因可能是因爲正則表達式不是確定字符串是否是有效的數學方程的好工具!當您開始添加其他運算符(如括號,指數等)時會發生什麼? –

+0

...但是,如果我們將問題限制爲您提到的簡單類型的方程,那麼當然可以用正則表達式來完成。 –

+0

使用下面的正則表達式 \ d * \ s \ + \ s \ d * \ s –

回答

0

要匹配一個完整的表達,像2+3=246 - 4 = 2,像

^\d+\s*[+-]\s*\d+\s*=\s*\d+$ 

一個正則表達式會做。看看你在哪裏玩的example 1

如果你想匹配像2+3+4+5=14更長的表達式,那麼你可以使用:

^\d+\s*([+-]\s*\d+\s*)+=\s*\d+$ 

說明:

^\d+     # first operand 
\s*     # 0 or more spaces 
(      # start repeating group 
[+-]\s*    # the operator (+/-) followed by 0 or more spaces 
\d+\s*    # 2nd (3rd,4th) operand followed by 0 or more spaces 
)+      # end repeating group. Repeat 1 or more times. 
=\s*\d+$    # equal sign, followed by 0 or more spaces and result. 

現在,你可能要接受像2=2表達式作爲一個有效的表達。在這種情況下,重複組可以是不存在,所以改變+*

^\d+\s*([+-]\s*\d+\s*)*=\s*\d+$ 

example 2爲那一個。

1

嘗試:

^(?:\d+\s*[+-])*\s*\d+$ 

Demo

說明:

  • ^$錨正則表達式的整個字符串匹配
  • 我添加了\s*以允許每個數字/運算符之間有空格。
  • 我已將[0-9]替換爲\d只是爲了簡化它;這兩個是相同的。

我有點不清楚你是否願意允許/禁止包括= <digits>末,因爲你的問題中提到這一點,但你試圖properExpression表達不嘗試。如果是這種情況,應該很容易看到如何修改表達式來支持它。

+0

請注意,這不會通過'2+ 2-3' – kaza

+0

也不需要使用'^'/'$'作爲輸入只是掃描儀的一行,'matches'方法試圖匹配整行OP) – kaza

+0

'^(?:\ s * \ d + \ s * [+ - ])* \ s * \ d + $'將匹配「2+ 2-3」。 –

0

請注意,我沒有試圖解決任何除正則表達式問題之外的任何潛在問題。 儘可能地嘗試保持你的邏輯流程。雖然還有其他更有效的答案,但是你必須改變你的邏輯流程。

請參閱下面的內容,如果您有任何疑問,請告知我們。

static String properExpression = "\\s*(\\d+\\s*[+-]\\s*)*\\d+\\s*"; 
static String noInput = ""; 
static String numbersWithSpaces = ".*\\d[\\s]+\\d.*"; 
//I've tried also "[\\d\\s+\\d]"; 

static void validateUserInput() { 
    Scanner sc = new Scanner(System.in); 
    System.out.println("Enter a calculation."); 
    String input = sc.nextLine(); 

    if(input.matches(properExpression)) { 
     input=input.replaceAll(" ", ""); //You've to assign it back to input. 
     calculator.calculate();   //Hope you have a way to pass input to calculator object 
    } else if(input.matches(noInput)) { 
     System.out.print(0); 
    } else if(input.matches(numbersWithSpaces)) { 
      System.out.println("Check the numbers. It seems that there is a space between the digits"); 
    } else 
    System.out.println("sth else"); 

樣品工作版本here


說明

下面允許更換的空間..

\\s*  //Allow any optional leading spaces if any 
(  //Start number+operator sequence 
\\d+  //Number 
\\s*  //Optional space 
[+-]  //Operator 
\\s*  //Optional space after operator 
)*  //End number+operator sequence(repeated) 
\\d+  //Last number in expression 
\\s*  //Allow any optional space. 

數字與空間

.*   //Any beginning expression 
\\d  //Digit 
[\\s]+  //Followed by one or more spaces 
\\d  //Followed by another digit 
.*   //Rest of the expression 
+1

Thx!是的,當然,我有一個計算器類與計算方程。具有空格的數字的正則表達式也可以工作,我想我必須有一些時間與常規表達式單獨:D – pavelcik

相關問題