RegexMBS

I’m trying to use the MBS Regex plugin but I’m finding it very complex (compared to Xojo’s native but I think inferior implementation).

If I have a string like this:

"I am Garry Pettet. Not just Garry."

and a regular expression like this (to just match my first name exactly):

dim pattern = "Garry"

How can I get the start and end positions of the two “Garry” substrings? They should be [5, 10] and [28, 33]. I’ve only been able to manage to get the start and end offset of the first matching “Garry” with this code:

[code]const PATTERN = "Garry
const TEST = “I am Garry Pettet. Not just Garry.”
dim r as new RegExMBS

if not r.Compile(PATTERN) then MsgBox(“Failed”)

dim offsetCount as Integer = r.Execute(TEST) ’ <— This returns 1

dim offsets() as Integer
for i as Integer = 0 to offsetCount ’
offsets.Append(r.OffsetCharacters(i))
next i
[/code]

Trouble is the offsets array only contains [5, 10]. I’m not sure where I’m going wrong. Can anybody help?

First you need to Execute twice to find all positions.
Second offset 5 tells you to skip 5 characters till Garry, which ends at position 10.
Offsets are zero based.

Call Execute than again with start position 10.

Wow, great service!

So do I just keep calling Execute until Execute returns 0 if I want to find all matches in a string?

Well, yes, just as the example on the RegExMBS website.

How did I miss that? Thanks

I love MBS and the convenience the plugins offer when I need it. I am very curious about your comparison of the versions of RegEx. What makes the MBS plugin better than the built in Xojo stuff?

Primarily it’s the support for named capture groups

I would say: Speed

With MBS you can optimise a search. This is especially good when you want to run the same search multiple times. The more times, then more you benefit! I used this searching for the same pattern of text with multiple files.

I’ve been toying with the RegexMBS class for a few hours and am making progress.

Where I am struggling is how to find the character position of matches within a numbered group or named group. For example, suppose I have this contrived pattern: code![/code]. It should match a name followed by an exclamation point and store the matched name in the surname group.

If I have this string: My name is Garry Pettet! Not Tony Stark! then I am able to loop (using Execute) and get the matched names (“Pettet” and “Stark”) using RegexMBS.Substring("surname") but I don’t know how to find the start and length of the matches (i.e. “Pettet” is 6 characters long and starts at position 17 and “Stark” starts at position 34 and is 5 characters long). I can’t use RegexMBS.OffsetCharacters() because that only returns the position of the full match, which in the case of this pattern would be “Pettet!” and “Stark” (note the exclamation mark).

Is there a property or method I’m missing here?

and your named substring isn’t in the offsets after the capture?