|
|
| Next: [PATCH 0/3 v2] [GIT PULL] remove unlikelys |
| Author |
Message |
Justin C External

Since: Mar 25, 2009 Posts: 3
|
Posted: Wed Mar 25, 2009 1:10 pm Post subject: regex query Archived from groups: uk>comp>os>linux (more info?) |
|
|
I've been given a regex that matches white space between the last \w on
a line and the \n at the end of the line. The regex is:
\s+(?=\n)
I understand the \s+, what is going on with the (?=\n) ?
Thank you for any help you can give with this.
Justin.
--
Justin C, by the sea. |
|
| Back to top |
|
 |
chris External

Since: Feb 08, 2005 Posts: 28
|
Posted: Wed Mar 25, 2009 1:10 pm Post subject: Re: regex query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Justin C wrote:
> I've been given a regex that matches white space between the last \w on
> a line and the \n at the end of the line. The regex is:
>
> \s+(?=\n)
>
> I understand the \s+, what is going on with the (?=\n) ?
>
> Thank you for any help you can give with this.
It's the look-ahead. It finds the pattern by inspecting the surroundings
but without matching to them. |
|
| Back to top |
|
 |
Owen Rees External

Since: Nov 17, 2004 Posts: 9
|
Posted: Wed Mar 25, 2009 2:10 pm Post subject: Re: regex query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
On Wed, 25 Mar 2009 15:47:38 -0000, Justin C
<justin.0903 DeleteThis @purestblue.com> wrote in <6017.49ca521a.9742@zem>:
>I understand the \s+, what is going on with the (?=\n) ?
It is "A zero-width positive lookahead assertion" to quote the perl
regex manual. It means that the match will succeed if the next thing is
a newline (in this case) but without including the newline in what is
matched.
You need to be a bit careful with these because not all regex systems
support them.
--
Owen Rees
[one of] my preferred email address[es] and more stuff can be
found at <http://www.users.waitrose.com/~owenrees/index.html> |
|
| Back to top |
|
 |
Justin C External

Since: Mar 25, 2009 Posts: 3
|
Posted: Thu Mar 26, 2009 7:10 am Post subject: Re: regex query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
On 2009-03-25, Owen Rees <orees DeleteThis @hotmail.com> wrote:
> On Wed, 25 Mar 2009 15:47:38 -0000, Justin C
><justin.0903 DeleteThis @purestblue.com> wrote in <6017.49ca521a.9742@zem>:
>
>>I understand the \s+, what is going on with the (?=\n) ?
>
> It is "A zero-width positive lookahead assertion" to quote the perl
> regex manual. It means that the match will succeed if the next thing is
> a newline (in this case) but without including the newline in what is
> matched.
>
> You need to be a bit careful with these because not all regex systems
> support them.
Thanks Owen, and Chris. I've just read about that in `perldoc perlre`,
that's complicated stuff! (at least for me). It does lead to a whole
bunch more reading because of references to perlvars with which I wasn't
familiar either.
I think I just need to remember that I can do 'look-ahead' (and behind)
matches in perl, and then look them up in the docs when I need them. I'm
never going to remember this stuff!
Anyway, thanks again.
Justin.
--
Justin C, by the sea. |
|
| Back to top |
|
 |
chris External

Since: Feb 08, 2005 Posts: 28
|
Posted: Thu Mar 26, 2009 8:10 am Post subject: Re: regex query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Justin C wrote:
> On 2009-03-25, Owen Rees <orees RemoveThis @hotmail.com> wrote:
>> On Wed, 25 Mar 2009 15:47:38 -0000, Justin C
>> <justin.0903 RemoveThis @purestblue.com> wrote in <6017.49ca521a.9742@zem>:
>>
>>> I understand the \s+, what is going on with the (?=\n) ?
>> It is "A zero-width positive lookahead assertion" to quote the perl
>> regex manual. It means that the match will succeed if the next thing is
>> a newline (in this case) but without including the newline in what is
>> matched.
>>
>> You need to be a bit careful with these because not all regex systems
>> support them.
>
> Thanks Owen, and Chris. I've just read about that in `perldoc perlre`,
> that's complicated stuff! (at least for me). It does lead to a whole
> bunch more reading because of references to perlvars with which I wasn't
> familiar either.
>
> I think I just need to remember that I can do 'look-ahead' (and behind)
> matches in perl, and then look them up in the docs when I need them. I'm
> never going to remember this stuff!
That's what I do. I think I've only ever used it a couple of times, so
keeping it in my 'memory cache' is wasteful  |
|
| Back to top |
|
 |
Geoffrey Clements External

Since: Apr 06, 2004 Posts: 226
|
Posted: Thu Mar 26, 2009 9:10 am Post subject: Re: regex query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
"Justin C" <justin.0903 DeleteThis @purestblue.com> wrote in message
news:5598.49cb4d38.2b457@zem...
> On 2009-03-25, Owen Rees <orees DeleteThis @hotmail.com> wrote:
>> On Wed, 25 Mar 2009 15:47:38 -0000, Justin C
>><justin.0903 DeleteThis @purestblue.com> wrote in <6017.49ca521a.9742@zem>:
> I think I just need to remember that I can do 'look-ahead' (and behind)
> matches in perl, and then look them up in the docs when I need them. I'm
> never going to remember this stuff!
>
In reading this thread I discovered:
http://www.regular-expressions.info/
Looks like a very useful site.
--
Geoff |
|
| Back to top |
|
 |
Bruce Stephens External

Since: Aug 19, 2004 Posts: 186
|
Posted: Thu Mar 26, 2009 3:10 pm Post subject: Re: regex query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
Justin C <justin.0903 RemoveThis @purestblue.com> writes:
[...]
> I think I just need to remember that I can do 'look-ahead' (and behind)
> matches in perl, and then look them up in the docs when I need them. I'm
> never going to remember this stuff!
I wouldn't worry too much. I suspect even frequent regexp users would
want to use them only very rarely. |
|
| Back to top |
|
 |
Justin C External

Since: Mar 25, 2009 Posts: 3
|
Posted: Fri Mar 27, 2009 7:10 am Post subject: Re: regex query [Login to view extended thread Info.] Archived from groups: per prev. post (more info?) |
|
|
On 2009-03-26, Geoffrey Clements <geoffrey.clementsNO.DeleteThis@SPAMbaesystems.com> wrote:
> "Justin C" <justin.0903.DeleteThis@purestblue.com> wrote in message
> news:5598.49cb4d38.2b457@zem...
>> On 2009-03-25, Owen Rees <orees.DeleteThis@hotmail.com> wrote:
>>> On Wed, 25 Mar 2009 15:47:38 -0000, Justin C
>>><justin.0903.DeleteThis@purestblue.com> wrote in <6017.49ca521a.9742@zem>:
>> I think I just need to remember that I can do 'look-ahead' (and behind)
>> matches in perl, and then look them up in the docs when I need them. I'm
>> never going to remember this stuff!
>>
>
> In reading this thread I discovered:
> http://www.regular-expressions.info/
>
> Looks like a very useful site.
Thanks for the pointer, it looks quite a useful site. I've book-marked
it for future reference.
Justin.
--
Justin C, by the sea. |
|
| Back to top |
|
 |
|
|
|
You can post new topics in this forum You can reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
| |
|
|