Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expression to match a single occurance of a ? 1

Status
Not open for further replies.

gacaccia

Technical User
May 15, 2002
258
US
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 " pattern will be found, matching entire string. However, pattern will also be found with " 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?
 
Alternativelly if the regexp doesn't work you can use String.split() method, for example:
Code:
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] SplitExample {
  [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] main(String[] args) {
     [COLOR=#804040][b]for[/b][/color] (String s: args) {
        Integer numOfQuestionMark = [COLOR=#ff00ff]0[/color];

        [COLOR=#804040][b]if[/b][/color] (s.contains([COLOR=#ff00ff]"?"[/color])) {
          String[] strList = s.split([COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\\[/color][COLOR=#ff00ff]?"[/color]);
          numOfQuestionMark = strList.length - [COLOR=#ff00ff]1[/color]; 
        }
        System.out.println([COLOR=#ff00ff]"String [/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff]"[/color] + s + [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff] contains "[/color]
          + numOfQuestionMark.toString() + [COLOR=#ff00ff]" character [/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff]?[/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff]"[/color]);
     }
     
  }
}

Output:
Code:
C:\Work>javac SplitExample.java

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

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

C:\Work>java SplitExample "[URL unfurl="true"]http://somedomain.com?a=aaaaaaa?b=bbbbbbbbb"[/URL]
String "[URL unfurl="true"]http://somedomain.com?a=aaaaaaa?b=bbbbbbbbb"[/URL] contains 2 character "?"
 
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 "?"
 
Yeah, doing standard character counts isn't ideal because I will want to be checking other patterns in addition to the single ?.
 
If you only need RegExp that something like this:
Code:
[COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]class[/b][/color] RegexpExample {
  [COLOR=#2e8b57][b]public[/b][/color] [COLOR=#2e8b57][b]static[/b][/color] [COLOR=#2e8b57][b]void[/b][/color] main(String[] args) {
     String pattern = [COLOR=#ff00ff]"^[^/?]*[/?][^/?]*$"[/color];
     [COLOR=#804040][b]for[/b][/color] (String s: args) {
        [COLOR=#804040][b]if[/b][/color] (s.matches(pattern)) {
           System.out.println([COLOR=#ff00ff]"String [/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff]"[/color] + s + [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff] contains "[/color]
             + [COLOR=#ff00ff]"only one character [/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff]?[/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff]"[/color]);
        } 
        [COLOR=#804040][b]else[/b][/color] {
           System.out.println([COLOR=#ff00ff]"String [/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff]"[/color] + s + [COLOR=#ff00ff]"[/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff] doesn't contain "[/color]
             + [COLOR=#ff00ff]"only one character [/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff]?[/color][COLOR=#6a5acd]\"[/color][COLOR=#ff00ff]"[/color]);
        }
     }
     
  }
}

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 "?"
 
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!
 
Yes you modified it right - I had typo in the slashes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top