Home > Back-end >  Is is possible to use operators under literals?
Is is possible to use operators under literals?

Time:12-27

Suppose I have the following function

fx(label, text) = match(Regex("\\W*\\Q$label\\E\\s*(.*)"), text).captures[]
text1 = "IAREA aoi/IA_1.ias"

The result of the first function call

fx("IAREA (?!FILE)", text1)

is expected (probably with a wrong reason), because string "IAREA" is not followed by "FILE" in test1.

But the result of the second function call

fx("IAREA (?!MMM)",  text1) 

is not expected: Because "IAREA" in "text1" is NOT followed by "MMM", this function call should return aoi/IA_1.ias, but it returns nothing.

I'm wondering: Is it possible to achieve this by changing the label argument, not by changing the function body?

CodePudding user response:

Since you pass a regex pattern to the regex constructor, you needn't use the \Q and \E operators since all characters in between the two operators are treated as literal symbols.

You need to fix as follows:

Regex("\\W*$label\\s*(.*)")
  • Related