• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

pattern matching

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to match url like https://coderanch.com/forums/jforum?module=posts&action=insert&forum_id=1
using pattern class and matcher class


temp ="sdsdsdsds sdsds dsd https://coderanch.com/forums/jforum?module=posts&action=insert&forum_id=1 fgffgjgfgghfdfb vreervreccc thbvfcffgbfvhttps://coderanch.com/forums/jforum?module=posts&action=insert&forum_id=1";
Pattern pat = Pattern.compile( https://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)
 
Saloon Keeper
Posts: 8578
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

etika ahuja wrote:how to match url like https://coderanch.com/forums/jforum?module=posts&action=insert&forum_id=1
using pattern class and matcher class


temp ="sdsdsdsds sdsds dsd https://coderanch.com/forums/jforum?module=posts&action=insert&forum_id=1 fgffgjgfgghfdfb vreervreccc thbvfcffgbfvhttps://coderanch.com/forums/jforum?module=posts&action=insert&forum_id=1";
Pattern pat = Pattern.compile( https://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" );

 
Marshal
Posts: 22449
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 "https://coderanch.com" in the following String: "This site, https://coderanch.com, is cool!".
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic