Skip to content

Commit

Permalink
Convert all remaining *simple* cases of regex usage to re usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanrossum committed Oct 22, 1997
1 parent 426916e commit 9694fca
Show file tree
Hide file tree
Showing 23 changed files with 134 additions and 144 deletions.
9 changes: 5 additions & 4 deletions Lib/fmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,9 @@ def invert(self, d, region):
self.paralist[para2].invert(d, pos1, pos2)
#
def search(self, prog):
import regex, string
import re, string
if type(prog) == type(''):
prog = regex.compile(string.lower(prog))
prog = re.compile(string.lower(prog))
if self.selection:
iold = self.selection[0][0]
else:
Expand All @@ -474,8 +474,9 @@ def search(self, prog):
continue
p = self.paralist[i]
text = string.lower(p.extract())
if prog.search(text) >= 0:
a, b = prog.regs[0]
match = prog.search(text)
if match:
a, b = match.group(0)
long1 = i, a
long2 = i, b
hit = long1, long2
Expand Down
15 changes: 6 additions & 9 deletions Lib/fnmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
corresponding to PATTERN. (It does not compile it.)
"""

import re

_cache = {}

def fnmatch(name, pat):
Expand Down Expand Up @@ -42,11 +44,8 @@ def fnmatchcase(name, pat):

if not _cache.has_key(pat):
res = translate(pat)
import regex
save_syntax = regex.set_syntax(0)
_cache[pat] = regex.compile(res)
save_syntax = regex.set_syntax(save_syntax)
return _cache[pat].match(name) == len(name)
_cache[pat] = re.compile(res)
return _cache[pat].match(name) is not None

def translate(pat):
"""Translate a shell PATTERN to a regular expression.
Expand Down Expand Up @@ -85,8 +84,6 @@ def translate(pat):
stuff = stuff[1:] + stuff[0]
stuff = '[' + stuff + ']'
res = res + stuff
elif c in '\\.+^$':
res = res + ('\\' + c)
else:
res = res + c
return res
res = res + re.escape(c)
return res + "$"
13 changes: 6 additions & 7 deletions Lib/fpformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
# digits_behind: number of digits behind the decimal point


import regex
import re

# Compiled regular expression to "decode" a number
decoder = regex.compile( \
'^\([-+]?\)0*\([0-9]*\)\(\(\.[0-9]*\)?\)\(\([eE][-+]?[0-9]+\)?\)$')
decoder = re.compile( \
'^([-+]?)0*([0-9]*)((\.[0-9]*)?)(([eE][-+]?[0-9]+)?)$')
# \0 the whole thing
# \1 leading sign or empty
# \2 digits left of decimal point
Expand All @@ -30,10 +30,9 @@
# fraction is 0 or more digits
# expo is an integer
def extract(s):
if decoder.match(s) < 0: raise NotANumber
(a1, b1), (a2, b2), (a3, b3), (a4, b4), (a5, b5) = decoder.regs[1:6]
sign, intpart, fraction, exppart = \
s[a1:b1], s[a2:b2], s[a3:b3], s[a5:b5]
m = decoder.match(s)
if not m: raise NotANumber
sign, intpart, fraction, exppart = m.group(1, 2, 3, 5)
if sign == '+': sign = ''
if fraction: fraction = fraction[1:]
if exppart: expo = eval(exppart[1:])
Expand Down
6 changes: 3 additions & 3 deletions Lib/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import os
import fnmatch
import regex
import re


def glob(pathname):
Expand Down Expand Up @@ -50,7 +50,7 @@ def glob1(dirname, pattern):
return result


magic_check = regex.compile('[*?[]')
magic_check = re.compile('[*?[]')

def has_magic(s):
return magic_check.search(s) >= 0
return magic_check.search(s) is not None
18 changes: 9 additions & 9 deletions Lib/keyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
# To update the symbols in this file, 'cd' to the top directory of
# the python source tree after building the interpreter and run:
#
# PYTHONPATH=./Lib ./python Lib/keyword.py
#
# (this path allows the import of string.py and regexmodule.so
# for a site with no installation in place)
# python Lib/keyword.py

kwlist = [
#--start keywords--
Expand Down Expand Up @@ -52,7 +49,7 @@
iskeyword = kwdict.has_key

def main():
import sys, regex, string
import sys, re, string

args = sys.argv[1:]
iptfile = args and args[0] or "Python/graminit.c"
Expand All @@ -61,13 +58,15 @@ def main():

# scan the source file for keywords
fp = open(iptfile)
strprog = regex.compile('"\([^"]+\)"')
strprog = re.compile('"([^"]+)"')
lines = []
while 1:
line = fp.readline()
if not line: break
if string.find(line, '{1, "') > -1 and strprog.search(line) > -1:
lines.append(" '" + strprog.group(1) + "',\n")
if string.find(line, '{1, "') > -1:
match = strprog.search(line)
if match:
lines.append(" '" + match.group(1) + "',\n")
fp.close()
lines.sort()

Expand All @@ -90,4 +89,5 @@ def main():
fp.write(string.join(format, ''))
fp.close()

if __name__ == "__main__": main()
if __name__ == "__main__":
main()
9 changes: 5 additions & 4 deletions Lib/lib-old/fmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,9 +461,9 @@ def invert(self, d, region):
self.paralist[para2].invert(d, pos1, pos2)
#
def search(self, prog):
import regex, string
import re, string
if type(prog) == type(''):
prog = regex.compile(string.lower(prog))
prog = re.compile(string.lower(prog))
if self.selection:
iold = self.selection[0][0]
else:
Expand All @@ -474,8 +474,9 @@ def search(self, prog):
continue
p = self.paralist[i]
text = string.lower(p.extract())
if prog.search(text) >= 0:
a, b = prog.regs[0]
match = prog.search(text)
if match:
a, b = match.group(0)
long1 = i, a
long2 = i, b
hit = long1, long2
Expand Down
7 changes: 4 additions & 3 deletions Lib/mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import rfc822
import os
import regex

class _Mailbox:
def __init__(self, fp):
Expand Down Expand Up @@ -117,12 +116,13 @@ def _search_end(self):

class MHMailbox:
def __init__(self, dirname):
pat = regex.compile('^[0-9][0-9]*$')
import re
pat = re.compile('^[0-9][0-9]*$')
self.dirname = dirname
files = os.listdir(self.dirname)
self.boxes = []
for f in files:
if pat.match(f) == len(f):
if pat.match(f):
self.boxes.append(f)

def next(self):
Expand Down Expand Up @@ -187,6 +187,7 @@ def _test():
if not msg:
break
msgs.append(msg)
msg.fp = None
if len(args) > 1:
num = string.atoi(args[1])
print 'Message %d body:'%num
Expand Down
24 changes: 9 additions & 15 deletions Lib/mhlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
import os
import sys
from stat import ST_NLINK
import regex
import re
import string
import mimetools
import multifile
Expand Down Expand Up @@ -236,9 +236,9 @@ def deletefolder(self, name):

# Class representing a particular folder

numericprog = regex.compile('^[1-9][0-9]*$')
numericprog = re.compile('^[1-9][0-9]*$')
def isnumeric(str):
return numericprog.match(str) >= 0
return numericprog.match(str) is not None

class Folder:

Expand Down Expand Up @@ -906,15 +906,12 @@ def pickline(file, key, casefold = 1):
f = open(file, 'r')
except IOError:
return None
pat = key + ':'
if casefold:
prog = regex.compile(pat, regex.casefold)
else:
prog = regex.compile(pat)
pat = re.escape(key) + ':'
prog = re.compile(pat, casefold and re.IGNORECASE)
while 1:
line = f.readline()
if not line: break
if prog.match(line) >= 0:
if prog.match(line):
text = line[len(key)+1:]
while 1:
line = f.readline()
Expand All @@ -931,18 +928,15 @@ def updateline(file, key, value, casefold = 1):
f.close()
except IOError:
lines = []
pat = key + ':\(.*\)\n'
if casefold:
prog = regex.compile(pat, regex.casefold)
else:
prog = regex.compile(pat)
pat = re.escape(key) + ':(.*)\n'
prog = re.compile(pat, casefold and re.IGNORECASE)
if value is None:
newline = None
else:
newline = '%s: %s\n' % (key, value)
for i in range(len(lines)):
line = lines[i]
if prog.match(line) == len(line):
if prog.match(line):
if newline is None:
del lines[i]
else:
Expand Down
19 changes: 9 additions & 10 deletions Lib/nntplib.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


# Imports
import regex
import re
import socket
import string

Expand Down Expand Up @@ -313,13 +313,13 @@ def slave(self):
# - list: list of (nr, value) strings

def xhdr(self, hdr, str):
pat = re.compile('^([0-9]+) ?(.*)\n?')
resp, lines = self.longcmd('XHDR ' + hdr + ' ' + str)
for i in range(len(lines)):
line = lines[i]
n = regex.match('^[0-9]+', line)
nr = line[:n]
if n < len(line) and line[n] == ' ': n = n+1
lines[i] = (nr, line[n:])
m = pat.match(line)
if m:
lines[i] = m.group(1, 2)
return resp, lines

# Process an XOVER command (optional server extension) Arguments:
Expand Down Expand Up @@ -354,14 +354,13 @@ def xover(self,start,end):
# - list: list of (name,title) strings

def xgtitle(self, group):
line_pat = regex.compile("^\([^ \t]+\)[ \t]+\(.*\)$")
line_pat = re.compile("^([^ \t]+)[ \t]+(.*)$")
resp, raw_lines = self.longcmd('XGTITLE ' + group)
lines = []
for raw_line in raw_lines:
if line_pat.search(string.strip(raw_line)) == 0:
lines.append(line_pat.group(1),
line_pat.group(2))

match = line_pat.search(string.strip(raw_line))
if match:
lines.append(match.group(1, 2))
return resp, lines

# Process an XPATH command (optional server extension) Arguments:
Expand Down
10 changes: 5 additions & 5 deletions Lib/pipes.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@


import sys
import regex
import re

import os
import tempfile
Expand Down Expand Up @@ -124,10 +124,10 @@ def append(self, cmd, kind):
if self.steps <> [] and self.steps[-1][1] == SINK:
raise ValueError, \
'Template.append: already ends with SINK'
if kind[0] == 'f' and regex.search('\$IN', cmd) < 0:
if kind[0] == 'f' and not re.search('\$IN\b', cmd):
raise ValueError, \
'Template.append: missing $IN in cmd'
if kind[1] == 'f' and regex.search('\$OUT', cmd) < 0:
if kind[1] == 'f' and not re.search('\$OUT\b', cmd):
raise ValueError, \
'Template.append: missing $OUT in cmd'
self.steps.append((cmd, kind))
Expand All @@ -146,10 +146,10 @@ def prepend(self, cmd, kind):
if self.steps <> [] and self.steps[0][1] == SOURCE:
raise ValueError, \
'Template.prepend: already begins with SOURCE'
if kind[0] == 'f' and regex.search('\$IN\>', cmd) < 0:
if kind[0] == 'f' and not re.search('\$IN\b', cmd):
raise ValueError, \
'Template.prepend: missing $IN in cmd'
if kind[1] == 'f' and regex.search('\$OUT\>', cmd) < 0:
if kind[1] == 'f' and not re.search('\$OUT\b', cmd):
raise ValueError, \
'Template.prepend: missing $OUT in cmd'
self.steps.insert(0, (cmd, kind))
Expand Down
11 changes: 5 additions & 6 deletions Lib/plat-irix5/cddb.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,17 @@ def __init__(self, tracklist):
self.notes = []
if not hasattr(self, 'file'):
return
import regex
reg = regex.compile('^\\([^.]*\\)\\.\\([^:]*\\):[\t ]+\\(.*\\)')
import re
reg = re.compile(r'^([^.]*)\.([^:]*):[\t ]+(.*)')
while 1:
line = f.readline()
if not line:
break
if reg.match(line) == -1:
match = reg.match(line)
if not match:
print 'syntax error in ' + file
continue
name1 = line[reg.regs[1][0]:reg.regs[1][1]]
name2 = line[reg.regs[2][0]:reg.regs[2][1]]
value = line[reg.regs[3][0]:reg.regs[3][1]]
name1, name2, value = match.group(1, 2, 3)
if name1 == 'album':
if name2 == 'artist':
self.artist = value
Expand Down
Loading

0 comments on commit 9694fca

Please sign in to comment.