Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Regular expressions
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="David C. Fox, post: 1734442"] Dennis is correct: alphabetizing both the pattern and the target strings is way to do this. I would use a slightly different regex, though, constructed as follows: import re def char_counts(s): """returns a dictionary indicating how many times a given character appears in the string s """ d = {} for char in s: d[char] = d.get(char, 0) + 1 # current count, or zero, plus one return d def char_subset(s): """given a string s, returns a regular expression which matches a sorted character string containing a subset of the same characters (and with no more occurences of each character than in the original string) """ counts = char_counts(s) l = [] chars = counts.keys() chars.sort() for char in chars: r = '%s{0,%d}' % (char, counts[char]) l.append(r) # regex fragment matching count or fewer occurances os that many l.append('$') # make sure that we match against the full string return ''.join(l) def sorted_string(s): """given a string s, return a new one which all the characters sorted """ l = list(s) l.sort() return ''.join(l) Then, you can compare a given target string, t, against the string in yipee with: r = char_subset(yipee) t_sorted = sorted_string(t) if re.match(r, t_sorted): print 'found a match: %s' %t David [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Regular expressions
Top