Help w/ regex for parsing parameters

J

Julie

I have to process parameter input strings of key/value pairs /key=value where
value _may_ contain a slash /.

Here is a sample input string:

/def=gamma chain (FT2) /name=NULL /p_id=CF11423:SF7 /fn=Defense/receptor member
/process=Signal transduction /len=98


Here is the regex:

\s*(?<val>.*?)\s*=


Here is the output:

0: ""
1: "def"
2: "gamma chain (FT2) "
3: "name"
4: "NULL "
5: "p_id"
6: "CF11423:SF7 "
7: "fn"
8: "Defense"
9: "receptor member /process"
10: "Signal transduction "
11: "len"
12: "98"

Everything is fine *except* for the /fn value, I don't want it to split between
defense and receptor. Is this possible w/ a regex, or should I just code my
own split routine to handle this case?
 
P

Philip Rieck

I'm no regex guru, but you can try this one- works for your test string, and
gives key/value pairs as named groups.

/(?<key>[^=]*)=(?<value>.*?)(?=\s/|$)
 
J

Julie

Philip said:
I'm no regex guru, but you can try this one- works for your test string, and
gives key/value pairs as named groups.

/(?<key>[^=]*)=(?<value>.*?)(?=\s/|$)

Nice -- works like a charm! Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top