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


Groups > comp.lang.php > #15547

Re: Regular expression for search first substring

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.php
Subject Re: Regular expression for search first substring
Date 2015-06-29 00:02 +0200
Organization PointedEars Software (PES)
Message-ID <2260000.HH7VSUHuKg@PointedEars.de> (permalink)
References <8396c942-b581-4721-9e54-db20a947d652@googlegroups.com> <mmomm3$gm4$1@dont-email.me>

Show all headers | View raw


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.

Back to comp.lang.php | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

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

csiph-web