I've spent some time on this problem but really need help from a regex guru.
So far I have the following which doesn't quite give me what I need.
[.*?]\s[=><]+\s[@]\w+
From the following sample string, I need all occurrences of a field followed by a parameter\variable. A parameter starts with an '@'.
I am then going to use the result to replace the contents of each value in .net.
Therefore, the regex expression would match
[System.TeamProject] = @project
[Microsoft.VSTS.Common.ClosedDate] >= @startOfDay
[Microsoft.VSTS.Common.ClosedDate] >= @startOfDay
Note [System.State] = 'Closed' is not matched.
Sample string
select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.Tags] from WorkItems where [System.TeamProject] = @project and [Microsoft.VSTS.Common.ClosedDate] >= @startOfDay and [System.State] = 'Closed' and [Microsoft.VSTS.Common.ClosedDate] >= @startOfDay
Thanks heaps!
This regex should do what you want:
\[([^]]*)]\s+[><=]+\s+(\@\w+)
The main change is that the [
and ]
in the initial part of your regex needed to be escaped. I have also added capture groups to collect the field name (group 1) and parameter value (group 2).
My guess is that maybe you're trying to write some similar expression to:
\[([^\]]*)\]\s*(=|>=|<=)\s*(@\w+)
and replace with some string similar to:
[new_value] $2 $3
I've added some capturing groups, wasn't sure about the desired output, you can simply remove or modify those if/as you wish.
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"\[([^\]]*)\]\s*(=|>=|<=)\s*(@\w+)";
string substitution = @"[new_value] $2 $3";
string input = @"select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.Tags] from WorkItems where [System.TeamProject] = @project and [Microsoft.VSTS.Common.ClosedDate] >= @startOfDay and [System.State] = 'Closed' and [Microsoft.VSTS.Common.ClosedDate] >= @startOfDay
Note [System.State] = 'Closed' is not matched.";
RegexOptions options = RegexOptions.Multiline;
Regex regex = new Regex(pattern, options);
string result = regex.Replace(input, substitution);
}
}
If you wish to simplify/modify/explore the expression, it's been explained on the top right panel of regex101.com . If you'd like, you can also watch in this link , how it would match against some sample inputs.
jex.im visualizes regular expressions:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.