INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Regular expression to match a single occurance of a ?

Regular expression to match a single occurance of a ?

Regular expression to match a single occurance of a ?

(OP)
I want to use a regular expression to confirm if a URL that includes query parameters only includes a single ?. So far, I have a pattern of "^http.*?\?[^\?]+", which sort of works, but fails to apply to the entire match string. For example, with "http://somedomain.com?a=aaaaaaa&b=bbbbbbbbb", pattern will be found, matching entire string. However, pattern will also be found with "http://somedomain.com?a=aaaaaaa?b=bbbbbbbbb", matching up to and not including second "?". I tried including a end-of-string anchor ("^http.*?\?[^\?]+$"), but both variations still match. Any thoughts on how to do this?

RE: Regular expression to match a single occurance of a ?

Alternativelly if the regexp doesn't work you can use String.split() method, for example:

CODE

public class SplitExample {
  public static void main(String[] args) {
     for (String s: args) {
        Integer numOfQuestionMark = 0;

        if (s.contains("?")) {
          String[] strList = s.split("\\?");
          numOfQuestionMark = strList.length - 1; 
        }
        System.out.println("String \"" + s + "\" contains "
          + numOfQuestionMark.toString() + " character \"?\"");
     }
     
  }
} 

Output:

CODE

C:\Work>javac SplitExample.java

C:\Work>java SplitExample "http://somedomain.com&a=aaaaaaa&b=bbbbbbbbb"
String "http://somedomain.com&a=aaaaaaa&b=bbbbbbbbb" contains 0 character "?"

C:\Work>java SplitExample "http://somedomain.com?a=aaaaaaa&b=bbbbbbbbb"
String "http://somedomain.com?a=aaaaaaa&b=bbbbbbbbb" contains 1 character "?"

C:\Work>java SplitExample "http://somedomain.com?a=aaaaaaa?b=bbbbbbbbb"
String "http://somedomain.com?a=aaaaaaa?b=bbbbbbbbb" contains 2 character "?" 

RE: Regular expression to match a single occurance of a ?

I thought about this usage

CODE

C:\Work>java SplitExample "foo" "b?ar" "b?a?z"
String "foo" contains 0 character "?"
String "b?ar" contains 1 character "?"
String "b?a?z" contains 2 character "?" 

RE: Regular expression to match a single occurance of a ?

(OP)
Yeah, doing standard character counts isn't ideal because I will want to be checking other patterns in addition to the single ?.

RE: Regular expression to match a single occurance of a ?

If you only need RegExp that something like this:

CODE

public class RegexpExample {
  public static void main(String[] args) {
     String pattern = "^[^/?]*[/?][^/?]*$";
     for (String s: args) {
        if (s.matches(pattern)) {
           System.out.println("String \"" + s + "\" contains "
             + "only one character \"?\"");
        } 
        else {
           System.out.println("String \"" + s + "\" doesn't contain "
             + "only one character \"?\"");
        }
     }
     
  }
} 

Output:

CODE

C:\Work>javac RegexpExample.java

C:\Work>java RegexpExample "foo" "b?ar" "b?a?z"
String "foo" doesn't contain only one character "?"
String "b?ar" contains only one character "?"
String "b?a?z" doesn't contain only one character "?" 

RE: Regular expression to match a single occurance of a ?

(OP)
that helped. i believe the example is incorrect on the forward slashes, however. should be "^[^\\?]*[\\?][^\\?]*$". having said that, i modified my original pattern to "^http:[^\?]*[\?][^\?]+$" and that worked.

thanks!

RE: Regular expression to match a single occurance of a ?

Yes you modified it right - I had typo in the slashes.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close