Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 293
Chase CB Avatar asked Jul 03 '14 21:07

Chase CB


People also ask

How do you know if strings are equal in Julia?

The cmp() is an inbuilt function in julia which is used to return 0 if the both specified strings are having the same length and the character at each index is the same in both strings, return -1 if a is a prefix of b, or if a comes before b in alphabetical order and return 1 if b is a prefix of a, or if b comes before ...

How do you add two strings in Julia?

Using '*' operator It is used to concatenate different strings and/or characters into a single string. We can concatenate two or more strings in Julia using * operator.

How do you split strings in Julia?

Similarly, you can use split() to split the string and count the results: julia> split(s, "") 55-element Array{Char,1}: '/' 'U' 's' 'e' 'r' 's' '/' ...

How do you replace a character in a string in Julia?

replace() Method Julia provides an inbuilt replace() function in Julia to replace a word or character with the specified string or character. where its parameters are str1, an abstract string, a pattern that is the word to be replaced, and count, which is of integer type. It returns a new sentence with replaced words.


1 Answers

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)
like image 99
kwinkunks Avatar answered Oct 26 '22 04:10

kwinkunks