RegexMBS

  1. 2 years ago

    Garry P

    16 Feb 2018 Pre-Release Testers, Xojo Pro Europe (Torquay, UK)

    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:

    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

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

  2. Christian S

    16 Feb 2018 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    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.

  3. Garry P

    16 Feb 2018 Pre-Release Testers, Xojo Pro Europe (Torquay, UK)

    Wow, great service!

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

  4. Christian S

    16 Feb 2018 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

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

  5. Garry P

    16 Feb 2018 Pre-Release Testers, Xojo Pro Europe (Torquay, UK)
    Edited 2 years ago

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

    How did I miss that? Thanks

  6. Tim P

    16 Feb 2018 Pre-Release Testers Rochester, NY

    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?

  7. Garry P

    16 Feb 2018 Pre-Release Testers, Xojo Pro Europe (Torquay, UK)

    @Tim P 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

  8. Christian S

    16 Feb 2018 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

    I would say: Speed

  9. David C

    16 Feb 2018 Pre-Release Testers, Xojo Pro Derby, ITM

    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.

  10. Garry P

    17 Feb 2018 Pre-Release Testers, Xojo Pro Europe (Torquay, UK)

    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: (?<surname>\w+)!. 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?

  11. Christian S

    17 Feb 2018 Pre-Release Testers, Xojo Pro, XDC Speakers, Third Party Store Germany

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

or Sign Up to reply!