Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

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.
 
Eat that pie! EAT IT! Now read this tiny ad. READ IT!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic