Chase CB
Chase CB

Reputation: 1627

Julia Regular Expressions

I'm trying to compare two lists and would like to use regular expression to do just that. Accordingly, I'd like to loop through the elements of one list and compare it to each of the elements in the other list. I can't seem to figure out how to make my regular expression contain a variable. Hopefully, this code should elucidate the matter:

string1="chase"

string2="chasecb"

m=match(r"$string1"  ,string2)

println(m)

I know that the $ is a regular expression metacharacter and I've tried escaping it and various permutations of that idea and so forth. Is there another way? Thanks so much.

Upvotes: 7

Views: 6320

Answers (2)

Matt Hall
Matt Hall

Reputation: 8152

As jverzani said in a comment, you can use Regex(string1) or Regex("$string1") to interpolate into a regular expression, for example:

string1 = "chase"
string2 = "chasecb"
m = match(Regex(string1), string2)
println(m)

Upvotes: 2

789
789

Reputation: 738

You need yo use the Regex() functions instead of r. For example:

string1="chase"

string2="chasecb"

m=match(r"$string1"  ,string2)

println(m)

For more details you can take a look at: here

Upvotes: 1

Related Questions