VMware Cloud Community
mcfadyenj
Hot Shot
Hot Shot
Jump to solution

regex appears to be buggy

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?

Reply
0 Kudos
1 Solution

Accepted Solutions
cdecanini_
VMware Employee
VMware Employee
Jump to solution

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));
 

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter

View solution in original post

Reply
0 Kudos
13 Replies
Andreas_Diemer
Enthusiast
Enthusiast
Jump to solution

Hi  mcfaydenj,

my search pattern for this would be

\$\([A-Z]*\)

to find all combinations of $() up to $(<filled with CPATIALS>)

  • \$ to ecape the meta character $
  • \( to escape bracket
  • [A-Z] to define a group of characters
  • * for repetition from 0 to ...
  • \) to escape bracket

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

------ for correct and / or useful answers please award points visit http://www.vcoteam.info & http://mighty-virtualization.blogspot.com
mcfadyenj
Hot Shot
Hot Shot
Jump to solution

Hi Andreas,

I tried this as I am using as expression.

\\$\\([A-Z_0-9]*\\)

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.

Reply
0 Kudos
Andreas_Diemer
Enthusiast
Enthusiast
Jump to solution

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

------ for correct and / or useful answers please award points visit http://www.vcoteam.info & http://mighty-virtualization.blogspot.com
Andreas_Diemer
Enthusiast
Enthusiast
Jump to solution

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

------ for correct and / or useful answers please award points visit http://www.vcoteam.info & http://mighty-virtualization.blogspot.com
Reply
0 Kudos
mcfadyenj
Hot Shot
Hot Shot
Jump to solution

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.

Reply
0 Kudos
cdecanini_
VMware Employee
VMware Employee
Jump to solution

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));
 

If my answer resolved or helped you, please mark it as Correct or Helpful to award points. Thank you! Visit http://www.vcoteam.info & http://blogs.vmware.com/orchestrator for vCenter Orchestrator tips and tutorials - @vCOTeam on Twitter
Reply
0 Kudos
Andreas_Diemer
Enthusiast
Enthusiast
Jump to solution

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  Smiley Wink

Regards, Andreas

------ for correct and / or useful answers please award points visit http://www.vcoteam.info & http://mighty-virtualization.blogspot.com
Reply
0 Kudos
Andreas_Diemer
Enthusiast
Enthusiast
Jump to solution

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

  • g (like /../g in JavaScript) for global search
  • i (like /../i) for case independend search
  • gi as the combination of both

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

------ for correct and / or useful answers please award points visit http://www.vcoteam.info & http://mighty-virtualization.blogspot.com
Reply
0 Kudos
mmarinov
VMware Employee
VMware Employee
Jump to solution

Thanks Christophe for this.

Just a tune of the pattern - the correct one should be

\$\(.[^\)]*\)

Regards,

--Martin

Martin Marinov VMware Software Engineer If you found this or any other answer useful please consider the use of the Helpful or correct buttons to award points
Reply
0 Kudos
Andreas_Diemer
Enthusiast
Enthusiast
Jump to solution

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

------ for correct and / or useful answers please award points visit http://www.vcoteam.info & http://mighty-virtualization.blogspot.com
Reply
0 Kudos
mmarinov
VMware Employee
VMware Employee
Jump to solution

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

Martin Marinov VMware Software Engineer If you found this or any other answer useful please consider the use of the Helpful or correct buttons to award points
Reply
0 Kudos
tschoergez
Leadership
Leadership
Jump to solution

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 Heart :

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

Reply
0 Kudos
mcfadyenj
Hot Shot
Hot Shot
Jump to solution

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.

Reply
0 Kudos