This week's book giveaway is in the OCP forum.
We're giving away four copies of OCP Java SE 8 Programmer II Exam Study Guide and have Kathy Sierra, Bert Bates, & Elizabeth Robson on-line!
See this thread for details.
Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex pattern  RSS feed

 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Suppose i have a url http://*.abc.com and i want to extract * from it, * can be anything, what would be the regex pattern for it
 
Marshal
Posts: 58384
178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Youknow we don't simply hand out answers. Besides, I don't know offhand. There are lots of regular expression resources; I presume you have been through the Java Tutorials which is a good introduction. Another useful link is http://www.regular-expressions.info/.
 
Sheriff
Posts: 23451
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see why you need a regex at all. You are just looking for the substring which you get by dropping the first 7 characters and the last 8 characters.
 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

But the abc.com can be anything.

Thanks,
 
Paul Clapham
Sheriff
Posts: 23451
46
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Jacob Sonia wrote:But the abc.com can be anything.



Anything? Then it isn't possible to produce such a regex. All we know about the text is that it's http:// followed by something-1 followed by a dot followed by something-2. And since both of something-1 and something-2 can themselves contain a dot, the requirements are ambiguous. You'll need to improve on your requirements before you can get anywhere.
 
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So basically you need everything after the first dot?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are given "http://foo.bar.abc.com" do you want to extract "foo" or "foo.bar"?

Either way, you can do this with just string manipulation. No regex necessary. Just do a substring.

You just need to find the right indices to pass in, and String has plenty of methods to figure those out.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!