Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33553 > unrolled thread
| Started by | Chris Angelico <rosuav@gmail.com> |
|---|---|
| First post | 2012-11-20 07:42 +1100 |
| Last post | 2012-11-20 07:42 +1100 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Robust regex Chris Angelico <rosuav@gmail.com> - 2012-11-20 07:42 +1100
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-11-20 07:42 +1100 |
| Subject | Re: Robust regex |
| Message-ID | <mailman.9.1353357750.29569.python-list@python.org> |
On Tue, Nov 20, 2012 at 7:32 AM, Joseph L. Casale
<jcasale@activenetwerx.com> wrote:
> Trying to robustly parse a string that will have key/value pairs separated
> by three pipes, where each additional key/value (if more than one exists)
> will be delineated by four more pipes.
>
> string = 'key_1|||value_1||||key_2|||value_2'
> regex = '((?:(?!\|\|\|).)+)(?:\|\|\|)((?:(?!\|\|\|).)+)(?:\|\|\|\|)?'
>
> I am not convinced this is the most effective or safest, any opinions would
> be greatly appreciated!
Is regex a requirement? Since you posted this on python-list, I'm
going to assume you're working in Python.
string = 'key_1|||value_1||||key_2|||value_2'
content = dict(map(lambda x: x.split("|||"),string.split("||||")))
--> {'key_1': 'value_1', 'key_2': 'value_2'}
ChrisA
Back to top | Article view | comp.lang.python
csiph-web