2016-07-26 25 views
0

我想匹配在scala中使用匹配的數學運算。因此,該函數將能夠匹配,如「5 + 2」或「LOG10」或任何字符串「10^5」等,但比賽一直未能爲各個類型的表達式在scala中使用正則表達式匹配數學運算

def isValid(expression:String):Boolean={ 

    val number = """((\-|\+)?[0-9]+\.?[0-9])*""" 
    val operation = """([\+,\-,*,/,C,P])""" 
    val functions = """(log|ln|sin|cos|tan|arc sin|arc cos|arc tan|sec|csc|cot)""" 
    val powers = """\^"""+number 

    val arithmeticExpression = (number + operation + number).r 
    val functionExpression = (functions + number).r 
    val powerOperation = (number + powers).r 
    val stringToTest: Regex = ("""(""" +arithmeticExpression+"""|"""+functionExpression+"""|"""+powerOperation+""")""").r 

    expression match { 
     case arithmeticExpression(s) => true 
     case functionExpression(s) => true 
     case powerOperation(s)=>true 
     case _ => false 

    } 
} 


println(isValid("1+4").toString) 

但是如果我匹配對於一般的表情,我得到預期的輸出:

def isValid(expression:String):Boolean={ 
    val number = """(\-|\+)?[0-9]+\.?[0-9]*""" 
    val operation = """[\+,\-,*,/,C,P]""" 
    val functions = """(log|ln|sin|cos|tan|arc sin|arc cos|arc tan|sec|csc|cot)""" 
    val power = """\^"""+number 
    val arithmeticExpression = number+operation+number 
    val functionExpression = functions+number 
    val powerExpression = number+power 

    val validExpression = """(""" +arithmeticExpression+"""|"""+functionExpression+"""|"""+powerExpression+""")""" 
    validExpression.r.findFirstIn(expression) match { 
     case Some(`expression`) => true 
     case None => false 
    } 

回答

1

你不是在做正確的數字:

scala> arithmeticExpression.findFirstIn("1+4") 
res2: Option[String] = Some(+)      

scala> arithmeticExpression.unapplySeq("1+4") 
res3: Option[List[String]] = None 

scala> arithmeticExpression.unapplySeq("11+14") 
res4: Option[List[String]] = Some(List(11, null, +, 14, null)) 

既然你需要兩個數字。

+0

有網站嘗試正則表達式,這是痛苦的看看和撰寫。如果有一個庫你可以在REPL中加載來拉開正則表達式等,那將會很好。另外,如果你對它們不感興趣,可以使用'case r(_ *)=>'忽略匹配組。 –

+0

是的括號混淆了表達式。謝謝! – Dguye

0

數字正則表達式中的「()」影響結果。還需要包裝在()中。這最終爲我工作。

def isValid(expression:String):Boolean={ 
    val number = """[\-,\+]?[0-9]+\.?[0-9]*""" 
    val operation = """([\+,\-,*,/,C,P])""" 
    val functions = """(log|ln|sin|cos|tan|arc sin|arc cos|arc tan|sec|csc|cot)""" 
    val powers = """(\^)"""+number 
    val arithmeticExpression = (""""""+number + operation + number+"""""").r 
    val functionExpression = (functions + number).r 
    val powerOperation = (number + powers).r 
    val stringToTest: Regex = ("""(""" +arithmeticExpression+"""|"""+functionExpression+"""|"""+powerOperation+""")""").r 

    expression match { 
     case arithmeticExpression(s) => { 
     println("Arithmetic Match") 
     true 
     } 
     case functionExpression(s) => { 
     println("Function Match") 
     true 
     } 
     case powerOperation(s)=>{ 
     println("Power Match") 
     true 
     } 
     case _ => false 

    } 
} 

感謝您的幫助!

+0

我不認爲你的「數字」RE退出。它似乎允許',7.'(逗號 - 點 - 點)作爲可接受的數字。也許這有點接近需要的東西:''「」[ - +]?\ d +(?:\。\ d +)?「」「' – jwvh

+0

謝謝,我認爲[]中的符號必須用逗號分隔 – Dguye