Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #90896
| Date | 2015-05-19 11:30 -0500 |
|---|---|
| From | Tim Chase <python.list@tim.thechases.com> |
| Subject | Re: Help with Regular Expression |
| References | <960db3a7-54e8-40dc-83f3-4e7e8f675529@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.154.1432069261.17265.python-list@python.org> (permalink) |
On 2015-05-19 06:42, massi_srb@msn.com wrote:
> I succesfully wrote a regex in python in order to substitute all
> the occurences in the form $"somechars" with another string. Here
> it is:
>
> re.sub(ur"""(?u)(\$\"[^\"\\]*(?:\\.[^\"\\]*)*\")""", newstring,
> string)
The expression is a little more precise than you describe it, but the
general idea is correct.
For the record, the "(?u)" happens to be unneeded here, and I find it
more clear if you pass the re.UNICODE flag to the function.
> Now I would need to exclude from the match all the string in the
> form $", ", can anyone help me to modufy it? Thanks in advance!
If you don't want commas or spaces, you should be able to just insert
them into your various negated character-classes:
r"""(?u)(\$\"[^\"\\, ]*(?:\\.[^\"\\, ]*)*\")"""
Unless you want to allow commas and/or spaces, but disallow commas
followed by spaces. That's a lot uglier. If that's the case, it
would help to have a test-harness of your expected inputs and results:
re_to_test = r"""(?u)(\$\"[^\"\\, ]*(?:\\.[^\"\\, ]*)*\")"""
for test, expected in [
('Hello $"who"!', 'Hello world!'),
('Hello $"who.who"!', 'Hello world!'),
('Hello $"who.is.it"!', 'Hello world!'),
('Hello $"who, what"!', 'Hello world!'),
('Hello $"who,what,where"!', 'Hello world!'),
('Hello $"who, what, where"!', 'Hello $"who, what, where"!'),
('Hello $"who is it"!', 'Hello world!'),
]:
result = re.sub(re_to_test, "world", test, re.UNICODE)
if result == expected:
report_passing(...)
else:
report_failure(...)
-tkc
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Help with Regular Expression massi_srb@msn.com - 2015-05-19 06:42 -0700 Re: Help with Regular Expression Tim Chase <python.list@tim.thechases.com> - 2015-05-19 11:30 -0500
csiph-web