0

I have incoming data something like this

http://localhost:1111/search?id=10&time=3200&type=abc
http://localhost:1111/search?time=3200&id=11&type=abc
http://localhost:1111/search?id=12
http://localhost:1111/search?id=13&time=3200&type=abc

The data is varying but not something completely random or unpredictable.

So basically how do we extract what are the IDs that are incoming in each string ignoring rest of the junk?

5
  • @DaveNewton, I gave sample data. It's not the exact data. :) More or less same phenomenon Commented Jan 7, 2014 at 16:39
  • Then it's misleading to show only URLs. Without knowing any of the data's constraints it's difficult to provide concrete advice. Commented Jan 7, 2014 at 16:41
  • i don't know how to do substring matching in Java but I'm sure it's there and easy to do with a regex such as the following: .+?id=(\d+).*? Commented Jan 7, 2014 at 16:42
  • 2
    "Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." Jamie Zawinski Commented Jan 7, 2014 at 16:42
  • 1
    @Reddy your sample data seems to suggest you are working with parameters... not sure if you could just work with ServletRequest or HttpServletRequest objects directly? Commented Jan 7, 2014 at 16:46

6 Answers 6

2

You can try using the regex id=(\d+) and extract the value of the first capturing group:

String url = "http://localhost:1111/search?id=10&time=3200&type=abc";

Pattern id = Pattern.compile("id=(\\d+)");

Matcher m = id.matcher(url);
if (m.find())
    System.out.println(m.group(1));
10

See Pattern and Matcher.

Sign up to request clarification or add additional context in comments.

4 Comments

This is awesome. Simply works. I always thought Pattern works in some other ways where we need to include complex regex pattern :)
@Reddy The regex you want seems to be pretty straightforward :) Glad I could help.
@arashajii, one more help, if I want extract 'type'? I tried similar, is not working.
@Reddy Well \d matches digits, and it looks like type is composed of non-digits as well. You can try "type=(\\w+)".
2

What if there are several ID's that are passed (which is valid)?

IMHO I wouild rather do somethis more like this:

URL url = new URL(<your link>);
String queryString = url.getQuery();

parse queryString into map for example of <String,List<String>> and get the value of ID key

Comments

1

(?<=[?&])id=(\d+)(?=(?:\&|$))

works in Regex Buddy under the Java and Perl flavor, but not in TextPad, which uses the Boost regex engine. Boost has issues with back-references.

(?<=(?:
   [?&]    //PRECEDED BY a question-mark or ampersand
))          
   id=(\d+) //"id=[one-or-more-digits]"
(?=(?:
   \&|$     //FOLLOWED BY an ampersand or the end of the input
))

This captures the digits only, and avoids issues such as capturing incorrect fields like

anotherid=123sometext

Comments

1

Expanding on @user1631616's answer:

Here is a sample code:

public static void main(String[] args) throws MalformedURLException {         
    URL aURL = new URL("http://localhost:1111/search?id=10&time=3200&type=abc");

    HashMap<String, String> params = new HashMap<>();
    String[] query = aURL.getQuery().split("&");
    for(String s: query) {
        String[] split = s.split("=");
        params.put(split[0],split[1]);
    }
    System.out.println(params.get("id")); 
    System.out.println(params.get("type")); 
    System.out.println(params.get("time")); 

}

That way if your HashMap param returns null you know that value was not set on the query string.

And also don't have to worry about the ordering of the parameters.

Comments

1

Why exactly do you want to use a regular expression to do this?

I would do it like this:

String url = "http://localhost:1111/search?id=13&time=3200&type=abc";
     String[] split = url.split("&");
     String id = "";    
     for (String s : split){
         if (s.contains("id")){
             id = s.substring(s.indexOf("id=")+3, s.length());
         }
     }

     System.out.println(id);

13

Comments

0

Something like this should do what you want:

(?<=id=)\d+

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.