Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #2123
| From | Ney André de Mello Zunino <zunino@softplan.com.br> |
|---|---|
| Newsgroups | comp.programming |
| Subject | Regular expressions and rusty knowledge |
| Date | 2012-08-29 14:56 -0300 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <k1ll3j$jsa$1@speranza.aioe.org> (permalink) |
Hello. Yesterday at work, I was presented with strings like the following: a) foo b) foo=bar c) foo=bar;rate=high d) foo;rate=high e) foo;rate f) foo=bar;rate=high;pos g) foo=bar;rate=high;pos;brick=many . . . They are basically semicolon-separated pairs of key=value, where values may or may not be present. I thought I would try and come up with a regular expression to recognize and allow me to extract tokens from them. Here's a more detailed list of the restrictions of the language: 1. values are optional 2. the number of key/value pairs is not specified 3. no string can begin or end with ';' 4. no string can begin or end with '=' 5. if a key is followed by '=', a corresponding value must be present 6. for simplicity, assume keys and values are only made of [a-z] This is what I came up with: [a-z]+(=([a-z]+))?(;[a-z]+(=([a-z]+))?)* It seems to adhere to the restrictions and recognize valid strings as expected. However, I'm unsure whether my idea of using a RE like that to extract tokens is actually feasible (e.g. the value 'high' associated with the second key on sample string g). For a moment, I even questioned myself whether that was a regular language. In the end, I suspect the years that have passed since I got my CS degree have begun to show and that I'm simply missing something the basics here. Anyway, I'd be thankful if somebody could shed some light on this and help me remove the rust from my understanding of this subject. Thank you and regards, -- Ney André de Mello Zunino
Back to comp.programming | Previous | Next — Next in thread | Find similar | Unroll thread
Regular expressions and rusty knowledge Ney André de Mello Zunino <zunino@softplan.com.br> - 2012-08-29 14:56 -0300 Re: Regular expressions and rusty knowledge Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2012-08-29 11:19 -0700 Re: Regular expressions and rusty knowledge Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-08-30 00:12 +0100
csiph-web