www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - regex start-of-line

reply spir <denis.spir gmail.com> writes:
Hello,

I have a regex bug in following conditions: users pass a series of regex 
formats (strings) from which I create regex engines later used for lexing. To 
ensure matching at start of (rest of) source, I prefix each format with '^'. 
Right in most cases. But the following regex:
	`^(true)|(false)`
matches "false" anywhere in text ==> bug. Is this normal? There is a kind of 
ambiguity here, isn't there?

As a workaround, I replaced:
         this.engine = Regex("^" ~ form);
by
         this.engine = Regex(format("^(%s)", form));
And indeed
	`^((true)|(false))`
does not match "false" anywhere else as at start of source.

Comments?

Denis
-- 
_________________
vita es estrany
spir.wikidot.com
Feb 12 2011
next sibling parent Jesse Phillips <jessekphillips+D gmail.com> writes:
spir Wrote:

 Hello,
 
 I have a regex bug in following conditions: users pass a series of regex 
 formats (strings) from which I create regex engines later used for lexing. To 
 ensure matching at start of (rest of) source, I prefix each format with '^'. 
 Right in most cases. But the following regex:
 	`^(true)|(false)`
 matches "false" anywhere in text ==> bug. Is this normal? There is a kind of 
 ambiguity here, isn't there?
 
 As a workaround, I replaced:
          this.engine = Regex("^" ~ form);
 by
          this.engine = Regex(format("^(%s)", form));
 And indeed
 	`^((true)|(false))`
 does not match "false" anywhere else as at start of source.
 
 Comments?

"The pipe has the lowest precedence of all operators." http://www.regular-expressions.info/reference.html Based on this page you should be able to do `^(true|false)` But I wouldn't be surprised if D would use e|f "abc(def|xyz) matches abcdef or abcxyz"
Feb 12 2011
prev sibling parent spir <denis.spir gmail.com> writes:
On 02/12/2011 06:57 PM, Jesse Phillips wrote:
 "The pipe has the lowest precedence of all operators."

Right, I guess this answers my question :) Denis -- _________________ vita es estrany spir.wikidot.com
Feb 12 2011