regex - Regular expression for contents of parenthesis in Racket -
how can contents of parenthesis in racket? contents may have more parenthesis. tried:
(regexp-match #rx"((.*))" "(check)")
but output has "(check)" 3 times rather one:
'("(check)" "(check)" "(check)")
and want "check" , not "(check)".
edit: nested parenthesis, inner block should returned. hence (a (1 2) c) should return "a (1 2) c".
parentheses capturing , not matching.. #rx"((.*))"
makes 2 captures of everything. thus:
(regexp-match #rx"((.*))" "any text") ; ==> ("any text" "any text" "any text")
the resulting list has first whole match, first set of acpturnig paren , ones inside second.. if want match parentheses need escape them:
(regexp-match #rx"\\((.*)\\)" "any text") ; ==> #f (regexp-match #rx"\\((.*)\\)" "(a (1 2) c)") ; ==> ("(a (1 2) c)" "a (1 2) c")
now see first element whole match, since match might start @ location in search string , end match largest. second element 1 capture.
this fail if string has additional sets of parentheses. eg.
(regexp-match #rx"\\((.*)\\)" "(1 2 3) (a (1 2) c)") ; ==> ("(1 2 3) (a (1 2) c)" "1 2 3) (a (1 2) c")
it's because expression isn't nesting aware. aware of need recursive reguler expression in perl (?r)
syntax , friends, racket doesn't have (yet???)
Comments
Post a Comment