-
Notifications
You must be signed in to change notification settings - Fork 0
TemplateVariables
Sometimes you might find it difficult to specify proper RegEx expression in commandline due to special characters.
The first easiest approach is to try to escape special caracters by your shell rules (bash, cmd, PowerShell - they all have different rules)
Windows cmd: ^^^
For example, one of the most common is <
character because of positive lookbehind:
Problematic RegEx | Windows cmd | PowerShell | bash |
---|---|---|---|
(?<=Version=)\d.\d.\d |
(?^^^<=Version=)\d.\d.\d |
But this is not always reliable since sometimes you need different escaping in different circumstances (interactive or batch file). So, there are two more solutions introduced by grepl
One of obvious solution is to keep RegEx in a separate file that will be read directly without any escaping issues. For this you can use -f
same as in linux grep.
grepl -f my.rx *.csproj -r
But this intruduces another problem - a content of the file is hardcoded, so you got additional limitation for you automation scripts. To overcome this, you can use template variables in you RegEx patterns. The syntax for this variables is taken to have as less as possible interference with RegEx rules and shell special characters
(?'varname')
- introduces a variable in RegEx pattern
In .Net RegEx one of possible syntax for named groups is (?'name'pattern)
, so I decided to make use of this syntax with empty pattern to specify template variables
During the call you can pass an actual value for your automation script
grepl -f my.rx *.csproj -r --var lib System.Web
Based on the same variable approach you can make use of predefined variables instead of shell escaping.
Variable | Effect |
---|---|
q | " |
caret | ^ |
lt | < |
gt | > |
or | | |
o/o | % |
e.g. grepl (?<=Version=) *.csproj -r
can be escaped as grepl (?(?'lt')=Version=) *.csproj -r