Search Operation with Delimiters

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stadala
    New Member
    • Nov 2012
    • 1

    #1

    Search Operation with Delimiters

    I have the below scenario
    Search Function (Could be Fwd/Reverse Direction)

    Ex: 2-6-281651 (Start-Offset-Value) or 2-6-"281-651"

    I was able to create code based on the split delimiter as (-), but if my search string has - in it, how can we handle.

    I can change the delimiter, but what if the new delimiter is part of the string, i can make sure that the value is in "",

    I am trying to create a generic solution, with out any limitations
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You could use your "surround it in quotes" solution. I'm unsure what your question is.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      You would not need the quotes using a regex solution. See if this works for you:
      Code:
      import re
      
      patt = re.compile(r"(\d+)-(\d+)-(.+)")
      
      s ='2-6-281-651'
      
      m = patt.match(s)
      if m:
          print m.group(1)
          print m.group(2)
          print m.group(3)
      
      s ='122-61-281651'
      
      m = patt.match(s)
      if m:
          print m.group(1)
          print m.group(2)
          print m.group(3)

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        I forgot about the maxsplit argument.
        Code:
        >>> s = '2-6-281-651'
        >>> s.split('-',2)
        ['2', '6', '281-651']
        >>>

        Comment

        • dwblas
          Recognized Expert Contributor
          • May 2008
          • 626

          #5
          Use a delimiter that won't be in any string like *#@.

          Comment

          Working...