2016-07-26 3 views
0

スカラーのマッチを使用して数学演算を照合しようとしています。だから、この関数は「5 + 2」または「LOG10」など任意の文字列にマッチさせることができるようになります「10^5」などしかし試合は表現が正規表現を使用してスカラーでの数学演算を一致させる

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)) 

を使用すると、2つの数字を必要としているので、一般的な表現のために私が期待される出力を取得します。

+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

関連する問題