• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using regular expression to extract parameters from URL

 
Ranch Hand
Posts: 43
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
let's say we have http://example.com/page?username=value&pwd=another

I need to extract username and pwd from the above URL using regular expression(not using string.split()).

Here's what I got

But this can only extract one parameter in this case only "username". How can I do something like "username" OR "pwd" =[^&]+ ??

I'm a bit weak in using regular expressions.
 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it possible to use something easier than regex?
Have you tried Request.getParameterMap() ?
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not String split? It'll be so much easier.
 
Arnob Dey
Ranch Hand
Posts: 43
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Tim Cooke

Oh, I tried that but for some reason its failing a few test cases (Maybe I'm doing it wrong!).



Note: I assumed that parameters will be placed successively in a definite order but that's not always the case which might be one of the reasons why some test cases are failing. And yes you may try using Map<String, String> as key-value pairs as well.

So I'm trying a different approach using regular expressions.
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arnob Dey wrote:you may try using Map<String, String> as key-value pairs as well.


Well that sounds like a splendid idea to me. I can't think of a scenario where the parameter order is important. If your tests are dependent on parameter order, then your tests are wrong.

A succession of splits should do the trick for you.

First split on '?' to separate the URL from the parameters

Second split the parameters on '&' to get a list of 'key=val' Strings

Third split on '=' to separate the key and value from each parameter.

Put the keys and values in a Map, and you're done.
 
Arnob Dey
Ranch Hand
Posts: 43
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Tim Cooke

Thanks..
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arnob Dey wrote: How can I do something like "username" OR "pwd" =[^&]+ ??


I know that there are suggestions already that are better than this, but I always feel obligated to point out that a SINGLE regex usually isn't the solution. There is nothing wrong with having several that each pick out a different part.

The major downsides to having one massive regex is that a) they are hard to write, b) they are hard to write CORRECTLY for all cases, c) they are impossible to understand 4 weeks later, d) they are hard to adapt/change.

if you instead have 3-4, it's easy to make methods for each:

getUsername()
getPassword()
getSomethingElse()
etc.

It's much easier to change 4 simple things than one complicated thing.
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:they are impossible to understand 4 weeks later


Wow Fred you have a great memory. I'd have forgotten how it works before the day is out,
 
Arnob Dey
Ranch Hand
Posts: 43
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Fred Rosenberger

Yes you are absolutely right..!! Good idea. Thanks.

 
reply
    Bookmark Topic Watch Topic
  • New Topic