Re: find and replace with regular expressions



On Jul 31, 3:07 pm, chrispoliq...@xxxxxxxxx wrote:
I am using regular expressions to search a string (always full
sentences, maybe more than one sentence) for common abbreviations and
remove the periods.  I need to break the string into different
sentences but split('.') doesn't solve the whole problem because of
possible periods in the middle of a sentence.

So I have...

----------------

import re

middle_abbr = re.compile('[A-Za-z0-9]\.[A-Za-z0-9]\.')

# this will find abbreviations like e.g. or i.e. in the middle of a
sentence.
# then I want to remove the periods.

----------------

I want to keep the ie or eg but just take out the periods.  Any
ideas?  Of course newString = middle_abbr.sub('',txt) where txt is the
string will take out the entire abbreviation with the alphanumeric
characters included.

middle_abbr = re.compile('[A-Za-z0-9]\.[A-Za-z0-9]\.')
s = 'A test, i.e., an example.'
a = middle_abbr.search(s) # find the abbreviation
b = re.compile('\.') # period pattern
c = b.sub('',a.group(0)) # remove periods from abbreviation
d = middle_abbr.sub(c,s) # substitute new abbr for old
d
'A test, ie, an example.'
.



Relevant Pages

  • Re: Searching a file
    ... Dim Abbreviation As String ... > So i am providing them a way to enter the shorthand text and I will ...
    (microsoft.public.vb.general.discussion)
  • Re: find and replace with regular expressions
    ... string will take out the entire abbreviation with the alphanumeric ... d = middle_abbr.sub(c,s) # substitute new abbr for old ...
    (comp.lang.python)
  • Re: oops i meant predictive text
    ... abbreviation with an entire string (phrase, name, sentence, etc.). ... find this helpful with firm names, ...
    (microsoft.public.word.customization.menustoolbars)
  • RE: String Comparison
    ... including the last words of both string expressions. ... abbreviation of the last word in the other string expr. ... > End Sub ... >> I want to perform a comparison on two string expressions. ...
    (microsoft.public.excel.programming)
  • Re: best practice?
    ... abbreviation on a page? ... It is part of the problem of poor design of that this question ... is not answered in the authoritative specifications. ... But the simple answer is that you should not use <abbr> at all. ...
    (alt.html)