Regular expression concatenation in julia
x = r"abc" y = r"def" z = join([x,y], "|") z # => r"r\"abc\"|r\"def\"" Is there a way to join (and generally manipulate) Regex , which deals only with the contents of the regular expression (i.e. does not process the r modifier as if it were part of the content). Desired result for z :
z # => r"abc|def" macro p_str(s) s end x = p"abc" y = p"def" z = Regex(join([x,y], "|")) The r "quote" statement actually compiles for you a regular expression that takes time. If you only have parts of the regular expression that you want to use to create a larger one, then you should store the details using "regular quotation marks."
But what about the thumbnail escaping rules that you get with r "quote" against regular quotes? If you want to get sketchy r "quote" rules, but don't immediately compile the regular expression, you can use a macro like:
macro p_str(s) s end Now you have p "quote", which comes out as r "quote", but just returns a string.
Do not leave the topic, but you can define a bunch of quotes in order to get around the tricky alphabets. Here are some handy ones:
# "baked\nescape" -> baked\nescape macro p_mstr(s) s end # p"""raw\nescape""" -> raw\\nescape macro dq_str(s) "\"" * s * "\"" end # dq"with quotes" -> "with quotes" macro sq_str(s) "'" * s * "'" end # sq"with quotes" -> 'with quotes' macro s_mstr(s) strip(lstrip(s)) end # s""" "stripme" """-> "stripme" When you finish making the fragments, you can make your join and make a regular expression, for example:
myre = Regex(join([x, y], "|")) Just as you thought.
If you want to know more about what the members of the object have (e.g. Regex.pattern), try:
julia> dump(r"pat") Regex pattern: ASCIIString "pat" options: Uint32 33564672 regex: Array(Uint8,(61,)) [0x45,0x52,0x43,0x50,0x3d,0x00,0x00,0x00,0x00,0x28 … 0x1d,0x70,0x1d,0x61,0x1d,0x74,0x72,0x00,0x09,0x00]