• 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 pattern

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have this program



I want to extract firstname.lastname from the string, my regex is not helping me, please help me with inputs.

Thanks,
Sonia
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You have a few issues with your regex... meaning it doesn't do what you think it does. Basically....


1. The square brackets are use to define character classes. Meaning [ABC] is a single character that is either A, B, or C -- it doesn't mean A followed by B followed by C.

2. The ^ character within a character class negates the match. Meaning [^ABC] is a single character that is NOT A, B, or C -- it doesn't mean the beginning of input.

Henry
 
Jacob Sonia
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you did not get- i understand that doing [^http..] would remove http and my intention is the same, i just want firstname.lastname
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although you might think so based on the output, the expression "[^http:\\/\\/]" is not skipping over the String "http://".

As Henry explained, it is only matching a single character that is not h, or t, or p, or :, or /.

So when you run this using your example of "http://firstname.lastname...", it looks like it's working, because the first single character that meets this criteria happens to be f.

But if your String was "http://phil.spector...", then the p and the h would also be skipped over, and the first single character that meets this criteria would be i.

After matching that first character, the expression ".*" matches any character zero or more times, and that's what's giving you the rest of your output. The attempts to negate ".blub.com/sdfsdf" are having no effect.
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic