Loading ...
Sorry, an error occurred while loading the content.
 

Regular Expressions ... using named subpatterns

Expand Messages
  • Don
    It has been a while since I ve done it ... I use this: ^!Replace ^ ( ){0,1}(?P .*?)( )*
    Message 1 of 2 , Aug 4, 2012
      It has been a while since I've done it ...

      I use this:
      ^!Replace "^<tr><td>(<a target="_blank"
      href="(?P<url>.*?)">){0,1}(?P<club>.*?)(</a>)*</td><td
      nowrap="nowrap">(?P<number>(\d|-){4,10})</td><td>(?P<city>.+),
      (?P<state>.*?)</td><td nowrap="nowrap">(?P<phone>.*?)(</td><td
      nowrap="nowrap" />|</td><td nowrap="nowrap">)(<a
      href="javascript:email\('(?P<edomain>.+?);(?P<email>.+?)'\);">e-mail</a></td>){0,1}<td>(?P<level>.+?)</td></tr>"
      >>
      "\k?<url>|\k?<club>|\k?<number>|\k?<city>|\k?<state>|\k?<phone>|\k?<edomain>|\k?<email>|\k?<level>"
      RAWS

      Help on pcre says:
      In PCRE, a subpattern can be named in one of three ways: (?<name>...) or
      (?'name'...) as in Perl, or (?P<name>...) as in Python.

      and

      There are several different ways of writing back references to named
      subpatterns. The .NET syntax \k{name} and the Perl syntax \k<name> or
      \k'name' are supported, as is the Python syntax (?P=name). Perl 5.10's
      unified back reference syntax, in which \g can be used for both numeric
      and named references, is also supported. We could rewrite the above
      example in any of the following ways: ...

      Output is this:
      k?<url>|k?<club>|k?<number>|k?<city>|k?<state>|k?<phone>|k?<edomain>|k?<email>|k?<level>
      k?<url>|k?<club>|k?<number>|k?<city>|k?<state>|k?<phone>|k?<edomain>|k?<email>|k?<level>
      k?<url>|k?<club>|k?<number>|k?<city>|k?<state>|k?<phone>|k?<edomain>|k?<email>|k?<level>

      The data pattern does not matter. Can't find an example in help ...
    • anachromat
      [struggling with named groups in regexp replacement] ... Note that back reference syntax has nothing to do with replacement syntax, so none of this is helping
      Message 2 of 2 , Aug 4, 2012
        [struggling with named groups in regexp replacement]
        > ...
        >
        > There are several different ways of writing back
        > references to named subpatterns.

        Note that back reference syntax has nothing to do with replacement syntax, so none of this is helping you ;-)

        In short, to use the group named X in a NoteTab replacement string, you must use this syntax:

        $<X>

        Here's an example, which displays ba (in NTP 7.0):

        ^!Info ^$StrReplace("(?P<n1>.)(?P<n2>.)";"$<n2>$<n1>";"ab";R)$

        This is documented in the "Substring Replaceements" section of one of the help files that shipped with your NoteTab. LOL! No, I don't remember which one ;-)
      Your message has been successfully submitted and would be delivered to recipients shortly.