Need a code example for Plugin API: RegexMatchAll()

Post your questions and problem reports here

Moderator: kfury77

Forum rules
Please try to follow these guidelines. This will help to receive faster and more accurate response.
  • Check the Support section of the corresponding product first. Chances are you will find your answer there;
  • Do not create new topics for already reported problems. Add your comments to the existing topics instead;
  • Create separate topic for each problem request. Do NOT post a number of non-related problem reports in a single topic;
  • Give your topic a meaningful title. Titles such as "A question," "Bug report" and "Help!" provide others no clue what your message is about;
  • Include the version number of the software you are using;
  • This is not an official customer support helpdesk. If you need a prompt and official response, please contact our support team directly instead. It may take a while until you receive a reply in the forum;
Post Reply
User avatar
pmk65
Posts: 678
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark

Need a code example for Plugin API: RegexMatchAll()

Post by pmk65 »

According to the Plugin API Documentation, the RegexMatchAll() function is described like this:

Format:
RegexMatchAll(s, RegEx, IgnoreCase, &Matches, &Positions)

Returns:
Bool (Which is either true or false (1/0), but I get 0 and -1 !!)

Decription:
Match all instances in string by regular expression.
Caseless matching if IgnoreCase is True.
Matches is array of array of string that receives all matched instances, containing substrings by regular expression groups.
Positions is array of array of integer that receives all matched instances, containing positions in base string of all matched regular expression groups.

When I do a "Length(Matches)" I get a positive value back, equal to the number of matches.
But then Im stuck, as I can't figure out how to iterate over the "Matches" results.
Everything I do just results in an error. :(

So could you PLEASE add a code example on how to iterate over the results from "RegexMatchAll" ?

Program used: WeBuilder 2015 v13.2.0.164
There are 10 types of people in the world: Those who understand binary and those who don't.
User avatar
Aivars
Blumentals Software Developer
Posts: 2462
Joined: Thu Aug 22, 2002 1:40 pm
Location: Latvia

Re: Need a code example for Plugin API: RegexMatchAll()

Post by Aivars »

I sent you answer via e-mail support but now I'm noticing you had asked the same here on forums so here's a copy of the answer which might be useful for someone else, too.

Here's a sample:

Code: Select all

      if (RegexMatchAll(s, "<value>(.*?)<\/value>", True, matches, poses)) {      
        if (Length(matches) > 0) {
          for (f = 0; f < Length(matches); f++) {   //iterate through all matches
            alert(_v(matches, [f, 1]));   // alert the first group of the match
          }
        }
      }
The trick is to use _v function to access members of the returned 2-dimensional array.

Regarding the return value, Windows interprets byte as 0 = false, anything else = true, and it's not uncommon to use -1 instead of 1, because binary representation of -1 is 1111...1.
Blumentals Software Programmer
User avatar
pmk65
Posts: 678
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark

Re: Need a code example for Plugin API: RegexMatchAll()

Post by pmk65 »

Thanks! The "_v()" stuff was just what I needed. (It's missing from the Plugin API docs, so no wonder I couldn't get it to work! :D)

I have another question regarding RegexMatchAll. I have a RegEx that has 3 captioning groups plus 1 optional group.
Currently I do this (using TRY/EXCEPT), as I don't know how to get the length/count of the capturing groups. (I hope you understand what I mean)
Is this the correct way to do this, or is there a way to get the length/count of the capturing groups?

Code: Select all

	if (RegexMatchAll(selection, regEx, true, matches, poses)) {
		var len = Length(matches);
		if (len > 0) {
			for (f = 0; f < len; f++) {   // iterate through all matches
				full = _v(matches, [f, 0]);	// Full match
				g1 = _v(matches, [f, 1]);	// 1st capturing group
				g2 = _v(matches, [f, 2]);	// 2nd capturing group
				g3 = _v(matches, [f, 3]);	// 3rd capturing group
				try {
					a = _v(matches, [f, 4]);	// Optional 4th capturing group
				}
				except {
					a = 1;
				}
			}
		}
	}
There are 10 types of people in the world: Those who understand binary and those who don't.
User avatar
Aivars
Blumentals Software Developer
Posts: 2462
Joined: Thu Aug 22, 2002 1:40 pm
Location: Latvia

Re: Need a code example for Plugin API: RegexMatchAll()

Post by Aivars »

Try/except is the best way to do it at this time.
Blumentals Software Programmer
User avatar
pmk65
Posts: 678
Joined: Sun Dec 20, 2009 9:58 pm
Location: Copenhagen, Denmark

Re: Need a code example for Plugin API: RegexMatchAll()

Post by pmk65 »

Aivars wrote:Try/except is the best way to do it at this time.
Ok. As long as it works :)
There are 10 types of people in the world: Those who understand binary and those who don't.
Post Reply