• Post Reply Bookmark Topic Watch Topic
  • New Topic

pattern matching  RSS feed

 
etika ahuja
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to match url like http://www.coderanch.com/forums/jforum?module=posts&action=insert&forum_id=1
using pattern class and matcher class


temp ="sdsdsdsds sdsds dsd http://www.coderanch.com/forums/jforum?module=posts&action=insert&forum_id=1 fgffgjgfgghfdfb vreervreccc thbvfcffgbfvhttp://www.coderanch.com/forums/jforum?module=posts&action=insert&forum_id=1";
Pattern pat = Pattern.compile( http://www.coderanch.com/forums/jforum?module=posts&action=insert&forum_id=1);
Matcher mat= pat.matcher(temp);

as in pattern matching ? is a quantifier then how to ignore ? as a quantifier and treat it as normal "?"(question mark)
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
etika ahuja wrote:how to match url like http://www.coderanch.com/forums/jforum?module=posts&action=insert&forum_id=1
using pattern class and matcher class


temp ="sdsdsdsds sdsds dsd http://www.coderanch.com/forums/jforum?module=posts&action=insert&forum_id=1 fgffgjgfgghfdfb vreervreccc thbvfcffgbfvhttp://www.coderanch.com/forums/jforum?module=posts&action=insert&forum_id=1";
Pattern pat = Pattern.compile( http://www.coderanch.com/forums/jforum?module=posts&action=insert&forum_id=1);
Matcher mat= pat.matcher(temp);

as in pattern matching ? is a quantifier then how to ignore ? as a quantifier and treat it as normal "?"(question mark)


Period (.) means "match any character".
Question mark means the preceding expression or character is optional (ie zero or one).
You can escape these by preceding them with a backslash. In Java String constants the backslash itself needs to be preceded by a backslash.
P.S. You're missing the enclosing double quotes.

Pattern pat = Pattern.compile( "http://www\\.coderanch\\.com/forums/jforum\\?module=posts&action=insert&forum_id=1" );

 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And to make it more general, look for anything from http:// or https:// until the first whitespace / String end. To make it a bit more complex you can also stop at the first punctuation character followed by whitespace or the String end. Positive lookaheads could help with that. A quick and untested attempt:
That looks impressive but isn't:
- http for the literal http
- (s?) for an optional s
- :// for the literal ://
- .*? for a non-greedy match-all
- the (/?) is an optional trailing slash; without this it is treated as punctuation
- the rest is the positive lookahead:
--- \\s for whitespace
--- or $ for the end of the String
--- or \\p{Punct} (a punctuation character) followed by whitespace or the end of the String

The latter causes to only match "http://www.coderanch.com" in the following String: "This site, http://www.coderanch.com, is cool!".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!