• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regex to extract a substring

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you could use the URL class and use its various getXXX methods to extract the parts you want.
 
If you want to look young and thin, hang around old, fat people. Or this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic