Menu

Get regex pattern from ParserElement

2007-08-01
2013-05-14
  • Joey Tallieu

    Joey Tallieu - 2007-08-01

    Is there a way to get the re.pattern string from a ParserElement?  I'd like to be able to see the regex generated from constructing ParserElements.

    Thanks,
    Joey

     
    • Paul McGuire

      Paul McGuire - 2007-08-01

      Joey -

      Sadly, you happened to choose just the form of Word that does *not* create an RE.  For the record, you asked about this expression:

      octet = Word(nums,None, 1,3)

      Try this:

      octet = Word(nums)
      print octet.reString

      I only generate RE's for Words that don't specify the min and max elements.  You will find other RE's generated within other pyparsing elements, such as oneOf, but many times, this is done only for special, easily-regex'ed forms.

      -- Paul

      (I'm fairly sure the re for this expression would be r"\d{1:3}" or "[0-9]{1:3}", depending on your preferences in RE syntax.)

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.