var array = new Array();
array = searchText.match(searchPattern);
System.log("getMatch array content:" + array);
if (array != null)
{
return array;
}
can anyone see why this does not seem to work properly.
my understanding is the string.match method returns an array.
so if i search something like this
test$(TESTA)test$(TESTB)
with a pattern such as this.
\\$\\([A-Z]\\) I would expect to find an array with 2 elements in it.
$(TESTA)
$(TESTB)
It seems to successfully find one element and I cannot see why. Is this a bug with the match function?
I am not really good at regex but I have your example working with this:
var test = "1234A34645B343534C455";
var re = new RegExp("[A-Z]", "g");
System.log(test.match(re));
Hi mcfaydenj,
my search pattern for this would be
\$\([A-Z]*\)
to find all combinations of $() up to $(<filled with CPATIALS>)
Do not double escape if string is used as value, only if it is used as an expression.
See also http://mighty-virtualization.blogspot.com/2011/02/vco-use-and-examples-of-regexp-regular.html
Regards, Andreas
Hi Andreas,
I tried this as I am using as expression.
I still get the same result only the first instance is found.
so after execution my array only contains $(TESTA) where as I expect $(TESTA), $(TESTB)
I had a + in place of the * earlier as I was under the impression the + was for repetition of chars etc as in c#.
This code is ported from a c# replica not implemented via vco. The same lookup works fine hence why I am a little confused.
It successfully gets the first instance and my understanding is the regex would return an array of known instances.
Hi again,
and sorry for the last (deleted) post - I was in HTML Java 🙂
(\\$\\([A-Z]*\\))+
is the pattern you looking for.
RegExp-engine looks for the most left match. If you dont specify a repetition you'll get only the first match.
var test = "test$(TESTA)test$(TESTB)";
System.log (test.match("(\\$\\([A-Z]*\\))+"));
results in following log
[2011-03-31 09:37:55.978] [I] $(TESTA),$(TESTA)
Regards, Andreas
Hi & sorry again,
I just mentioned, that my result in last post is buggy.
Checked string.match() with several RegExp and found a misbehavior too.
A simple example:
var test = "1234A34645B343534C455";
System.log (test.match("[A-Z]"];
I expect an array like this: A,B,C
But all I got is: A
I will forward this to Christophe & Burke.
Regards, Andreas
yeah i noticed your result also showed $(TESTA), $(TESTA) but I would expect A then B
I wont be at work for another 10 hours to test it. I really should get dial in access but the paperwork is just ridiculous so I can't be bothered.
I know the same regex in c# works like a charm. So I am pretty sure the expression is fine. But I still haven't tried your other example yet. anyway im off to bed so ill let you know tmrw.
cheers I appreciate all the help. its quite a tricky beast to come to grips with this vco. I think its starting to play nicely now just a few little teething issues like this to deal with.
I am not really good at regex but I have your example working with this:
var test = "1234A34645B343534C455";
var re = new RegExp("[A-Z]", "g");
System.log(test.match(re));
Hi Christophe,
this looks like the JavaScript /.../g for global search.
I will try the other parmeters like /../i and so on
So finally the string.match() explanation in vCO is not so fine / clear
Regards, Andreas
Hi,
like Chrisophe wrote, we have to add the parameter for additional search (like in HTML) in the RegExp
test = "12aUS33A232aus3B23442C2auS33";
pattern = "aus";
var res = test.match(new RegExp(pattern, "gi"));
System.log (res.length);
System.log (res);
finds all combinations of aus: aUS,aus,auS
so
var test = "test$(TESTA)test$(TESTB)";
var pattern = "\\$\\([A-Z]*\\)";
System.log test.match(new RegExp(pattern, "g"));
will solve your initial question
Kind Regards, Andreas
Thanks Christophe for this.
Just a tune of the pattern - the correct one should be
\$\(.[^\)]*\)
Regards,
--Martin
Hi Martin,
sorry for this, but the tune is wrong.
I mentioned the different behavior between value and expression above.
If you pass it from input it is a value. Inside a script it is always an expression.
Because the expression is evaluated you have to escape the escape.
Try it in lab and proof result.
Regards, Andreas
Hi Adreas,
Thanks for your comment. Yes you are correct if we discuss the escaping. The tune is for the way text is beeing patterned between the brackets.
So yes, if this tuning is not used as input parameter is must be
\\$\\(.[^\\)]*\\)
Regards,
--Martin
Hi Guys,
great discussion and good examples!
For further reference:
http://www.javascriptkit.com/jsref/regexp.shtml
http://www.regular-expressions.info/javascriptexample.html
And the books from O'reilly about regexp are mandatory in each programmers library :
http://www.amazon.com/Mastering-Regular-Expressions-Jeffrey-Friedl/dp/0596528124
http://www.amazon.com/Regular-Expressions-Cookbook-Jan-Goyvaerts/dp/0596520689
I'm still waiting for a real-world use-cae to validate a credit card number within vCO :smileygrin:
Regards,
Joerg
thank you very much to all concerned this is now working like a charm.
"g" certainly fixed things up. thanks Martin for the tuning of the regex.