Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #15542 > unrolled thread

Regular expression for search first substring

Started byJuan Garcia <hotmaildeme@gmail.com>
First post2015-06-27 09:53 -0700
Last post2015-06-29 00:02 +0200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  Regular expression for search first substring Juan Garcia <hotmaildeme@gmail.com> - 2015-06-27 09:53 -0700
    Re: Regular expression for search first substring Norman Peelman <npeelman@cfl.rr.com> - 2015-06-28 07:45 -0400
      Re: Regular expression for search first substring Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-06-29 00:02 +0200

#15542 — Regular expression for search first substring

FromJuan Garcia <hotmaildeme@gmail.com>
Date2015-06-27 09:53 -0700
SubjectRegular expression for search first substring
Message-ID<8396c942-b581-4721-9e54-db20a947d652@googlegroups.com>
Hi all,

this is a dumb question for any PHP developer, but i and regular expression not have feeling :)

Situation is string:

The boy love movies [any variable string] and sport but sport is more important.

I would like to search string "love movies...and sport" but regular expression i have return "love movies...and sport but sport".

Which is the correct regular expression for this?

Thanks all!

Sorry for my english.

[toc] | [next] | [standalone]


#15544

FromNorman Peelman <npeelman@cfl.rr.com>
Date2015-06-28 07:45 -0400
Message-ID<mmomm3$gm4$1@dont-email.me>
In reply to#15542
On 06/27/2015 12:53 PM, Juan Garcia wrote:
> Hi all,
>
> this is a dumb question for any PHP developer, but i and regular expression not have feeling :)
>
> Situation is string:
>
> The boy love movies [any variable string] and sport but sport is more important.
>
> I would like to search string "love movies...and sport" but regular expression i have return "love movies...and sport but sport".
>
> Which is the correct regular expression for this?
>
> Thanks all!
>
> Sorry for my english.
>

   You should supply the regex you are currently working with so that 
others can help you better. You don't really say exactly what you are 
after but this may get you started:

(love movies)(.+)(and sport)

love movies [any variable string] and sport

-- 
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-

[toc] | [prev] | [next] | [standalone]


#15547

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-06-29 00:02 +0200
Message-ID<2260000.HH7VSUHuKg@PointedEars.de>
In reply to#15544
Norman Peelman wrote:

> On 06/27/2015 12:53 PM, Juan Garcia wrote:
>> Situation is string:
>>
>> The boy love movies [any variable string] and sport but sport is more
>> important.
>>
>> I would like to search string "love movies...and sport" but regular
>> expression i have return "love movies...and sport but sport".
>>
>> Which is the correct regular expression for this?
> 
>    You should supply the regex you are currently working with so that
> others can help you better. You don't really say exactly what you are
> after but this may get you started:
> 
> (love movies)(.+)(and sport)
> 
> love movies [any variable string] and sport

Without flags, “.+” means “any string of at least one character that does 
not contain newline” in ERE/PCRE.  “Any variable string” is only matched by 
“.*” or “.+” with PCRE and the “s” flag, or by an all-character class like 
“[\S\s]” without either or both.

The parentheses cause storing of the matches for the subexpressions for 
later use of back-references.  They are a waste of runtime and memory if you 
are only matching against a string.

The OP’s problem is probably that they used

  /love movies.*sport/

or

  /love movies.+sport/

and did not consider that regular expressions are greedy by default, i.e. as 
much as possible is matched by them.  There are three ways to work around 
that:

  A) provide more context, as you indicated (by specifying the “and” as 
     well)

  B) make the expression non-greedy:

       /love movies.*?sport/

     or

       /love movies.+?sport/

  C) (only in PCRE) use negative lookahead or (here) lookbehind to prevent 
     the expression from matching if the match would contain certain 
     substrings:

       /love movies.+(?<! but )sport/

-- 
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web