www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Balanced match with std.regex

reply Jakob Ovrum <jakobovrum gmail.com> writes:
Is there a way to do balanced match with std.regex?

Example (from [1]):

test -> funcPow((3),2) * (9+1)

I want to match the funcPow((3),2) bit, regardless of the depth 
of the expression in funcPow(*).

https://stackoverflow.com/questions/7898310/using-regex-to-balan
e-match-parenthesis [1]
Dec 14 2015
next sibling parent reply Chris Wright <dhasenan gmail.com> writes:
On Tue, 15 Dec 2015 00:16:41 +0000, Jakob Ovrum wrote:

 Is there a way to do balanced match with std.regex?
 
 Example (from [1]):
 
 test -> funcPow((3),2) * (9+1)
 
 I want to match the funcPow((3),2) bit, regardless of the depth of the
 expression in funcPow(*).
 
 https://stackoverflow.com/questions/7898310/using-regex-to-balance-
match-parenthesis
 [1]
I don't think so. std.regex looks like it implements only a deterministic finite automaton, whereas what you are looking for requires a push-down automaton. It just so happens that a few popular regex libraries implement PDAs rather than DFAs (and have associated changes to their regex syntax to make it work, albeit inelegantly).
Dec 14 2015
parent reply Jakob Ovrum <jakobovrum gmail.com> writes:
On Tuesday, 15 December 2015 at 01:07:32 UTC, Chris Wright wrote:
 I don't think so.

 std.regex looks like it implements only a deterministic finite 
 automaton, whereas what you are looking for requires a 
 push-down automaton. It just so happens that a few popular 
 regex libraries implement PDAs rather than DFAs (and have 
 associated changes to their regex syntax to make it work, 
 albeit inelegantly).
Thanks, that makes sense. String manipulation in D without regex is pretty nice anyway, so it's not a big loss.
Dec 14 2015
parent reply Xinok <xinok live.com> writes:
On Tuesday, 15 December 2015 at 01:29:39 UTC, Jakob Ovrum wrote:
 Thanks, that makes sense.

 String manipulation in D without regex is pretty nice anyway, 
 so it's not a big loss.
There is a library named Pegged which can match against balanced parens: https://github.com/PhilippeSigaud/Pegged
Dec 14 2015
parent wobbles <grogan.colin gmail.com> writes:
On Tuesday, 15 December 2015 at 02:35:34 UTC, Xinok wrote:
 On Tuesday, 15 December 2015 at 01:29:39 UTC, Jakob Ovrum wrote:
 Thanks, that makes sense.

 String manipulation in D without regex is pretty nice anyway, 
 so it's not a big loss.
There is a library named Pegged which can match against balanced parens: https://github.com/PhilippeSigaud/Pegged
Pegged is amazeballs. Can build some v cool things with it. Particularly with some CTFE abuse!
Dec 15 2015
prev sibling parent crimaniak <crimaniak gmail.com> writes:
On Tuesday, 15 December 2015 at 00:16:41 UTC, Jakob Ovrum wrote:
 Is there a way to do balanced match with std.regex?
It's only possible with (?R) implemented: http://php.net/manual/en/regexp.reference.recursive.php But there is no (?R) in D regex implementation.
Dec 14 2015