Case-Sensitive Sarch and replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Philips

    Case-Sensitive Sarch and replace

    Using glob(), I obtain a list of filenames with some characters in
    upper case, others in lower case. I want to rename the files by
    replacing one substring in each filename with another, but with two
    twists.

    1. The search must be case insensitive
    2. After portion of the filename that does not match the search string
    must not have its case changed.

    For example, if fn="AlphaMin.tx t", searchstring="m in" and
    replacestring= "Max", I want the file to be renamed "AlphaMax.t xt" and
    not "alphaMax.t xt" or "alphamax.t xt"

    I can easily get alphaMax.txt by using
    fn.lower().repl ace(searchstrin g.lower(),repla cestring), but then the
    portion of fn that is not being replaced is lowercased.

    It's not hard to write a function that repeatedly finds
    searchstring.lo wer() in fn.lower(), and then uses slices to replace
    the appropriate portions of fn, but there must be a simpler and
    cleaner way to acheive this goal. Suggestions?

    Thomas Philips
  • Richie Hindle

    #2
    Re: Case-Sensitive Sarch and replace


    [Thomas][color=blue]
    > For example, if fn="AlphaMin.tx t", searchstring="m in" and
    > replacestring= "Max", I want the file to be renamed "AlphaMax.t xt" and
    > not "alphaMax.t xt" or "alphamax.t xt"[/color]

    Use case-insensitive regular expression replacement:
    [color=blue][color=green][color=darkred]
    >>> import re
    >>> def replace(filenam e, search, replace):[/color][/color][/color]
    .... regex = '(?i)' + re.escape(searc h)
    .... return re.sub(regex, replace, filename)
    ....[color=blue][color=green][color=darkred]
    >>> replace("AlphaM in.txt", "min", "Max")[/color][/color][/color]
    'AlphaMax.txt'

    --
    Richie Hindle
    richie@entrian. com


    Comment

    • Harry George

      #3
      Re: Case-Sensitive Sarch and replace

      tkpmep@hotmail. com (Thomas Philips) writes:
      [color=blue]
      > Using glob(), I obtain a list of filenames with some characters in
      > upper case, others in lower case. I want to rename the files by
      > replacing one substring in each filename with another, but with two
      > twists.
      >
      > 1. The search must be case insensitive
      > 2. After portion of the filename that does not match the search string
      > must not have its case changed.
      >
      > For example, if fn="AlphaMin.tx t", searchstring="m in" and
      > replacestring= "Max", I want the file to be renamed "AlphaMax.t xt" and
      > not "alphaMax.t xt" or "alphamax.t xt"
      >
      > I can easily get alphaMax.txt by using
      > fn.lower().repl ace(searchstrin g.lower(),repla cestring), but then the
      > portion of fn that is not being replaced is lowercased.
      >
      > It's not hard to write a function that repeatedly finds
      > searchstring.lo wer() in fn.lower(), and then uses slices to replace
      > the appropriate portions of fn, but there must be a simpler and
      > cleaner way to acheive this goal. Suggestions?
      >
      > Thomas Philips[/color]

      Try using regular expressions:

      data=["AlphaMin.txt", "alphaMin.txt", "Alphamin.t xt"]
      min_pat=re.comp ile(r'(min)',re .I)
      for name in data:
      msg("name=%s " % name)
      m=min_pat.searc h(name)
      cnt=len(m.group s())
      if cnt==0:
      msg('no match')
      elif cnt>1:
      msg('found more than one, need instructions')
      else:
      newname=min_pat .sub('Max',name )
      msg('newname=%s ' % newname)
      msg('\n')


      --
      harry.g.george@ boeing.com
      6-6M21 BCA CompArch Design Engineering
      Phone: (425) 342-0007

      Comment

      Working...