Use Variable in Regex (interpolate)

606 views
Skip to first unread message

Julia User

unread,
Mar 17, 2015, 5:14:35 PM3/17/15
to julia...@googlegroups.com
How to use a julia variable in a regex string (interpolate).

var = " "
reg
=  r"^$(var)+"   # Something like this
result
= matchall(reg, mystring)


Is there a way to make this work?

Thanks



David P. Sanders

unread,
Mar 17, 2015, 6:49:57 PM3/17/15
to julia...@googlegroups.com
How about the following? I don't know if there's a shorter way.


var
 = " "
reg_string 
=  "^$(var)+"   # standard Julia string
reg = Regex(reg_string)  # make a regular expression out of the string
result = matchall(reg, mystring)



Best,
David.

 

Thanks



Adrian Salceanu

unread,
Mar 6, 2016, 7:04:05 AM3/6/16
to julia-users
Great one, thanks! 

Re shorter way, it can be written as a one-liner: 

result = matchall(Regex("^$(var)+"), mystring)
Reply all
Reply to author
Forward
0 new messages