Regular expressions

Dealing with regular expressions is a very essential part of many text parsing operations. XSharper regex action is a wrapper around Regex .NET class, and is a block that executes once for every match, setting captures as variables prefixed with name attribute (if empty, and capture name is numeric, underscore is added ):


By default search uses IgnoreCase option. But options can be changed:


A piece of code may be executed if no matches were found:



Captures are available inside the regex block as variables. For captures without name, _1 variable is set for first capture, _2 for second, etc. _0 variable is set to the found string.


Output is below, demonstrating that by default capture variables are not propagated beyond the regex body:

To get regex just to set capture variables, setCaptures may be set. Additional attributes define that the loop should exit after the first match is found, and that all capture variables should get 'x:' prefix:


Output is now


replace attribute can be used to replace text

Replace with back-reference:


Note that the result is unexpected, because ${1} is expanded to '1' before being passed to regular expression. Can deal with this issue using a temp variable:


or by using a different escape sequence:


or by using an internal expression:


or by just using C#-like syntax: