[Pharo-project] Understanding regular expression matching

laurent laffont laurent.laffont at gmail.com
Sat Feb 12 09:01:06 CET 2011


'(\d+)' asRegex
search: 'value: 6474 mm';
subexpression: 1.
 answers 6474.

See
-
http://www.pharocasts.com/2010/05/dynamic-finder-with-doesnotunderstand.html
- https://gforge.inria.fr/frs/download.php/26818/Regex.pdf

Laurent Laffont - @lolgzs <http://twitter.com/#!/lolgzs>

Pharo Smalltalk Screencasts: http://www.pharocasts.com/
Blog: http://magaloma.blogspot.com/


On Fri, Feb 11, 2011 at 7:52 PM, Nick Ager <nick.ager at gmail.com> wrote:

> Hi,
>
> Is it possible to make a regular expression which matches only part of a
> string. For example:
>
> testRegex
> | toSearch matcher doesMatch match1 match2 numMatches |
>  toSearch := 'value: 6474 mm'.
>
> matcher := RxMatcher forString: ' (\d+) '.
>  doesMatch := matcher matches: toSearch.
> numMatches := matcher subexpressionCount.
>  match1 := (matcher subexpression: 1).
> match2 := (matcher subexpression: 2).
>  self halt.
>
> which results in:
>    doesMarch = false
>    numMatches = 2
>    match1 = nil
>    match2 = nil
>
> However if I replace the regular expression with: '.* (\d+) .*' so the
> whole string is matched I see
>
>    doesMarch = true
>    numMatches = 2
>    match1 = 'value: 6474 mm'
>    match2 = '6474'
>
> I can always pad my sub-matches with '.*', but I wondered if I'm missing a
> trick to make the Regex work with substring matches
>
> Thanks
>
> Nick
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gforge.inria.fr/pipermail/pharo-project/attachments/20110212/c50edfee/attachment.htm>


More information about the Pharo-project mailing list