Advertisement
James1337

StringPrepareExpression

Nov 23rd, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
AutoIt 1.69 KB | None | 0 0
  1. Func StringPrepareExpression($s, $d=False)
  2.     Local a = StringRegExpReplace, b = StringReplace
  3.     If $d Then ConsoleWrite("!!!  " & $s & @CRLF) ; debug
  4.     Local $v = StringRegExp($s, "\(([^()]+)\)", 3), $i, $f
  5.     If (Not @error) Then
  6.         For $i = 0 To UBound($v)-1
  7.             $s = StringReplace($s, "(" & $v[$i] & ")", "[$var" & $i & "]", 1, 1)
  8.             If $d Then ConsoleWrite("!!!  " & $v[$i] & " --> " & "$var" & $i & @CRLF) ; debug
  9.         Next
  10.         If $d Then ConsoleWrite("!!!  " & $s & @CRLF) ; debug
  11.         If (StringInStr($s, "(") > 0) Then $s = StringPrepareExpression($s, $d)
  12.     EndIf
  13.     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14.     Local $x = ".+?"
  15.     $s = "(" & $s & ")"
  16.     $s = a($s, b("\(\s*(x)\s*\+\s*(x)\s*\)", "x", $x), "Add[(\1), (\2)]")
  17.     $s = a($s, b("\(\s*(x)\s*\-\s*(x)\s*\)", "x", $x), "Sub[(\1), (\2)]")
  18.     $s = a($s, b("\(\s*(x)\s*\*\s*(x)\s*\)", "x", $x), "Mul[(\1), (\2)]")
  19.     $s = a($s, b("\(\s*(x)\s*\/\s*(x)\s*\)", "x", $x), "Div[(\1), (\2)]")
  20.     $s = a($s, b("\(\s*(x)\s*\%\s*(x)\s*\)", "x", $x), "Mod[(\1), (\2)]")
  21.     $s = a($s, b("\(\s*(x)\s*\^\s*(x)\s*\)", "x", $x), "Pow[(\1), (\2)]")
  22.     $s = StringReplace($s, "[", "(")
  23.     $s = StringReplace($s, "]", ")")
  24.     ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25.     If $d Then ConsoleWrite("!!!  " & $s & @CRLF) ; debug
  26.     If IsArray($v) Then
  27.         For $i = 0 To UBound($v)-1
  28.             $f = StringPrepareExpression($v[$i])
  29. ;~          $s = StringReplace($s, "$var" & $i, $f)
  30.             $s = StringReplace($s, "($var" & $i & ")", $f)
  31.             If $d Then ConsoleWrite("!!!  $var" & $i & " --> " & $f & @CRLF) ; debug
  32.         Next
  33.     EndIf
  34.     If $d Then ConsoleWrite("!!!  " & $s & @CRLF) ; debug
  35.     Return $s
  36. EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement