The APEX compiler works very different for RegEx than what you're used to with JS. First, none of the Global Switches are supported. Some things need to have additional escapes and pretty much everything needs to be in capture groups. That's because Apex uses different methods of it's own in the matcher class to provide this kind of functionality.
I believe you'll find that your (If|if)
needs to be replaced with an ((If)|(if))
.
With the way Apex works, you'll probably want to remove what appears to be a non-capture group: (?:\n|.)
To get you to the start of the name, I'd expect your pattern to look something more like the following:
(((<!)(\-*)(\\s*)((If)|(if)))\()
I recommend you go to http://regexr.com/ , paste in the string <!-- if(Rajendra Patel == 'Rajendra Patel'){
assuming that's what you want to match against, in the text box below (after clearing it), then work out the pattern for yourself.
I'd also suggest you think in terms of combining RegEX with other string methods to strip away things that are easy to find that you know you don't need or want. This will make your task easier.
For example, once you find your opening and closing parens inside the lesser and greater than symbols, you can strip away what's outside of them. You could also split the string based on finding the ==
. Be smart and don't make this more complex than it has to be.
First, &-.
matches all characters between &
and .
, including *
, (
, )
, '
, etc. Move the dash to either the beginning, the end, or escape it.
This behavior happens any time you have a character set where the pattern .-.
(where .
means any character) occurs, because that forms a character range (e.g. a-z
is a usual case, but it can actually be any two characters). This may cause some false positives/negatives.
Second, you can use the {}
operator to limit the number of appearances of stuff, and ()
groups to perform repeats to shorten your code. Your long form simply makes it harder to read.
Third, instead of using *
, I'd recommend using {,3}
if you want to limit the code to three characters per set.
Fourth, be aware that \w
also includes _
, so you might want to use the longer form of \w
minus the underscore.
This leads me to a final pattern of:
"([a-zA-Z0-9\\d&.\\/-]{,3}>){4}[a-zA-Z0-9&.\\/-]{,3}"
The rest of your formula looks okay, but you'll want to test this out to make sure. I wrote this as a validation rule in an object in my dev org, because I hate building processes just to test something like this, but it should work, at least in theory.
Best Answer
Try using the constructor syntax.
I've verified that this saves in lightning:
Note the escape of the
'
char within the expression.