School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,089 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,073 people online right now. Registration is fast and FREE... Join Now!




RegEx non-capture groups question

 

RegEx non-capture groups question

NickDMax

2 Nov, 2009 - 09:52 AM
Post #1

Can grep dead trees!
Group Icon

Joined: 18 Feb, 2007
Posts: 5,267



Thanked: 293 times
Dream Kudos: 1175
Expert In: Java/C++

My Contributions
Given: "hello world" and a regex: (?<!h)(\w+)

why does the regex find "hello"? I *thought* I was asking it to find words that don't begin with h.

Note I don't need a regex to find words that don't begin with h -- I am just trying to understand the negative lookback. If I wanted to find words that didn't begin with h I would use something like: [^h](\w+)


So the question is on using the (?<! ) non-capture group which does not seems to function the way I think it should (I have tested this in a number of languages so it is not a bug in the particular library as I initially thought).


User is offlineProfile CardPM
+Quote Post


NickDMax

RE: RegEx Non-capture Groups Question

2 Nov, 2009 - 01:53 PM
Post #2

Can grep dead trees!
Group Icon

Joined: 18 Feb, 2007
Posts: 5,267



Thanked: 293 times
Dream Kudos: 1175
Expert In: Java/C++

My Contributions
Sorry... as usual I discovered my answer on my own.

"hello" is a match for (\w+) and it is not preceded by an h...

however if the input string was "hhello world" we would match "hhello" because it is not preceded by an h (it is preceded by the start of the string).

didn't think it though


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 11:20AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month