Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex pattern

 
Jacob Sonia
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
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • 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/.
 
Paul Clapham
Sheriff
Posts: 21107
32
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: 21107
32
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.
 
David Newton
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?
 
Steven Schwab
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic