Help with regular expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sondar@gmail.com

    #1

    Help with regular expression

    Can anyone help me please?

    I have a regular expression "^A .* B$"

    If this matches against "A B A B" then how can I tell that ".*" was
    matched against "B A"?

    Thanks,
    Campbell
  • Alexander Mueller

    #2
    Re: Help with regular expression

    sondar@gmail.co m schrieb:
    Can anyone help me please?
    >
    I have a regular expression "^A .* B$"
    >
    If this matches against "A B A B" then how can I tell that ".*" was
    matched against "B A"?
    >
    Thanks,
    Campbell
    I am not familar to RegExp in .Net
    In VBScript and JavaScript you'd define a submatch by
    simply using paranthesis:

    'VBScript
    rx.Pattern = "^A( .* )B$"

    and return the part inbetween by accessing the submatch.
    There are multiple ways to do this, one is

    'VBScript
    sm = rx.replace("A B A B", "$1")



    I know dotnet uses a different term then 'submatch'.
    (i think they call it backreference instead ... not sure)
    and a different method for retrieving it, but the basic mechanism
    and the pattern are the same.

    MfG,
    Alex

    Comment

    • eBob.com

      #3
      Re: Help with regular expression

      I am certainly not a regex expert, but I think that what you need to search
      for is "capture".

      Good Luck, Bob

      <sondar@gmail.c omwrote in message
      news:91f74c6a-daea-4cfc-a4be-bb7d07b1a22c@e6 g2000prf.google groups.com...
      Can anyone help me please?
      >
      I have a regular expression "^A .* B$"
      >
      If this matches against "A B A B" then how can I tell that ".*" was
      matched against "B A"?
      >
      Thanks,
      Campbell

      Comment

      • sondar@gmail.com

        #4
        Re: Help with regular expression

        Thank you both, that was the push I needed. I needed to use named
        groups, i.e. change the pattern to

        "^A(?<mymatch>. *)B$"

        I can then use the re.Match().Grou ps collection to return the value of
        the mymatch group which in the case above would be "B A".

        Cheers,
        Campbell

        Comment

        Working...