[Pharo-project] Understanding regular expression matching

Schwab,Wilhelm K bschwab at anest.ufl.edu
Fri Feb 11 22:19:46 CET 2011


Have you looked at messages like #copyWithRegex:matchesTranslatedUsing:?  It might help??




________________________________________
From: pharo-project-bounces at lists.gforge.inria.fr [pharo-project-bounces at lists.gforge.inria.fr] On Behalf Of Nick Ager [nick.ager at gmail.com]
Sent: Friday, February 11, 2011 1:52 PM
To: pharo-project
Subject: [Pharo-project] Understanding regular expression matching

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




More information about the Pharo-project mailing list