正则表达式中的位置和长度

时间:2021-04-29 21:41:51

I have text like this:

我有这样的文字:

This is a sample {text}. I want to inform my {Dada} that I have some data which is {not useful}. So I need data to start by { and ends with }. This data needs to {find out}.

这是一个示例{text}。我想通知我的{Dada}我有一些{无用}的数据。所以我需要数据以{开头}结束。这些数据需要{找出}。

Total text have some substrings separated within curly braces {}. How can I find the starting position and length of the substring starting with { and ending with }? Further, I will replace the substring with the processed string.

总文本在花括号{}内分隔了一些子串。如何找到以{和以}结尾的子字符串的起始位置和长度?此外,我将用已处理的字符串替换子字符串。

2 个解决方案

#1


With Regex.Match, you can check the index of each match by accessing the Index property, and the length of each match by checking the Length property.

使用Regex.Match,您可以通过访问Index属性来检查每个匹配的索引,并通过检查Length属性来检查每个匹配的长度。

If you want to count the curly braces in, you can use \{(.*?)\} regex, like this:

如果你想计算花括号,你可以使用\ {(。*?)\}正则表达式,如下所示:

 var txt = "This is a sample {text}. I want to inform my {Dada} that I have some  data which is {not useful}. So I need data to start by { and ends with }. This data needs to {find out}.";
 var rgx1 = new Regex(@"\{(.*?)\}");
 var matchees = rgx1.Matches(txt);
 // Get the 1st capure groups
 var all_matches = matchees.Cast<Match>().Select(p => p.Groups[1].Value).ToList();
 // Get the indexes of the matches
 var idxs = matchees.Cast<Match>().Select(p => p.Index).ToList();
 // Get the lengths of the matches
 var lens = matchees.Cast<Match>().Select(p => p.Length).ToList();

Outputs:

正则表达式中的位置和长度正则表达式中的位置和长度正则表达式中的位置和长度

Perhaps, you will want to use a dictionary with search and replace terms, and that will be more effecient:

也许,你会想要使用带有搜索和替换术语的字典,这样会更有效:

var dic = new Dictionary<string, string>();
dic.Add("old", "new");
var ttxt = "My {old} car";
// And then use the keys to replace with the values
var output = rgx1.Replace(ttxt, match => dic[match.Groups[1].Value]);

Output:

正则表达式中的位置和长度

#2


If you know you will not have nested curly braces, you can use the following:

如果您知道不会嵌套花括号,则可以使用以下内容:

var input = @"This is a sample {text}. I want to inform my {Dada} that I have some data which is {not useful}. So I need data to start by { and ends with }. This data needs to {find out}."
var pattern = @"{[^]*}"
foreach (Match match in Regex.Matches(input, pattern)) {
    string subString = match.Groups(1).Value;
    int start = match.Groups(1).Index;
    int length = match.Groups(1).Length;
}

#1


With Regex.Match, you can check the index of each match by accessing the Index property, and the length of each match by checking the Length property.

使用Regex.Match,您可以通过访问Index属性来检查每个匹配的索引,并通过检查Length属性来检查每个匹配的长度。

If you want to count the curly braces in, you can use \{(.*?)\} regex, like this:

如果你想计算花括号,你可以使用\ {(。*?)\}正则表达式,如下所示:

 var txt = "This is a sample {text}. I want to inform my {Dada} that I have some  data which is {not useful}. So I need data to start by { and ends with }. This data needs to {find out}.";
 var rgx1 = new Regex(@"\{(.*?)\}");
 var matchees = rgx1.Matches(txt);
 // Get the 1st capure groups
 var all_matches = matchees.Cast<Match>().Select(p => p.Groups[1].Value).ToList();
 // Get the indexes of the matches
 var idxs = matchees.Cast<Match>().Select(p => p.Index).ToList();
 // Get the lengths of the matches
 var lens = matchees.Cast<Match>().Select(p => p.Length).ToList();

Outputs:

正则表达式中的位置和长度正则表达式中的位置和长度正则表达式中的位置和长度

Perhaps, you will want to use a dictionary with search and replace terms, and that will be more effecient:

也许,你会想要使用带有搜索和替换术语的字典,这样会更有效:

var dic = new Dictionary<string, string>();
dic.Add("old", "new");
var ttxt = "My {old} car";
// And then use the keys to replace with the values
var output = rgx1.Replace(ttxt, match => dic[match.Groups[1].Value]);

Output:

正则表达式中的位置和长度

#2


If you know you will not have nested curly braces, you can use the following:

如果您知道不会嵌套花括号,则可以使用以下内容:

var input = @"This is a sample {text}. I want to inform my {Dada} that I have some data which is {not useful}. So I need data to start by { and ends with }. This data needs to {find out}."
var pattern = @"{[^]*}"
foreach (Match match in Regex.Matches(input, pattern)) {
    string subString = match.Groups(1).Value;
    int start = match.Groups(1).Index;
    int length = match.Groups(1).Length;
}