0

I am looking for a Regex which will enable me to find something like &dog= or &entropy= in a string, I tried something like

&+s*^[a-zA-Z]+s*=

believing that I could separate the things I want with the s* but that didn't work out so well, any help would be appreciated. Thanks!

aydow
  • 3,673
  • 2
  • 23
  • 40
hiqbal
  • 87
  • 2
  • 9
  • 3
    Is this a URL you are trying to parse? – alecxe Aug 06 '15 at 16:50
  • Yeah but I can't use a Python specific library, this has to be done via Regex like I described above. Also I am not given entire urls , only parts of them. – hiqbal Aug 06 '15 at 17:08

2 Answers2

2

Im gonna go out on a ledge and assume

from urllib import urlparse
urlparse.parse_qs(my_query_string)
#or 
urlparse.urlparse(my_url_string)[3]

is what you really want

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Thanks for the suggestion but I have to do it with a regex because some of the strings I am given are only parts of urls and this can't be a Python specific library for constraints of the project. But thanks. – hiqbal Aug 06 '15 at 17:07
1

If you want to use a regex you can use something like this:

&\w+=

Working demo

Code

import re
p = re.compile(ur'&\w+=')
test_str = u"asdfasdf&asdf=asdfffff&abc123=asdf\n\n\n"

re.findall(p, test_str)

If you want to capture the value you can use capturing groups like this:

&(\w+)=
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123