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.
The matches
method will only return true if the entire input string matches the result. You want to use find
instead, which will match a substring. Take a look at the Matcher
documentation for these methods:
- find()
Attempts to find the next subsequence of the input sequence that matches the pattern. This method returns true if a subsequence of the input sequence matches this Matcher object's pattern.
- matches()
Attempts to match the entire region against the pattern.
So if you want to know whether your substring is contained anywhere in the input string, use MyMatcher.find()
instead.
A couple side notes:
- If you want your expression to be case insensitive, set the
i
flag by starting your expression with (?i)
.
- If you want to look for a whole word, wrap it in the word boundary character (
\b
), not backward and forward lookaheads for non-word characters.
- With the above in mind, I would change the expression to:
'(?i)\\b' + var + '\\b'
Best Answer
Try with this:
Copy and Paste Code: