• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex to extract a substring  RSS feed

 
Chandra shekar M
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

In the below Regex code

I get three groups http:// , abc@-1.com and 8080. So in this case I am matching all the three groups. Is there a way where i can directly extract only last two groups without matching the first group. say like this Pattern.compile((.*) : ([0-9]+)). but the problem with this is (.*) will also match the http:// and i'll get http://abc@-1.com and 8080. So i want to define a regex in such a way that i should be able to extract everything after http:// and out put should be abc@-1.com and 8080.
How can i do this?

Thanks
Chandra
 
Winston Gutkowski
Bartender
Posts: 10574
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandra shekar M wrote:In the below Regex code

Chandra,

I've split up that enormous line in your code. Please re-read the UseCodeTags (←click) page thoroughly.

Winston
 
Winston Gutkowski
Bartender
Posts: 10574
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandra shekar M wrote:So i want to define a regex in such a way that i should be able to extract everything after http:// and out put should be abc@-1.com and 8080.
How can i do this?

Just remove the brackets from the first expression. However, I wonder why you think you need to do this. With 3 groups you can still get the last two, so why bother?

One point: you may make things quite a bit faster by using the possessive qualifier and more specific search criteria, viz:
Pattern.compile("([^:]+://)?+([^:]*+):([0-9]+)")

Winston
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could use the URL class and use its various getXXX methods to extract the parts you want.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!