匹配连续数字的正则表达式
本文关键字:正则表达式 数字 连续 | 更新日期: 2023-09-27 17:49:22
我试图从以下连续数字字符串中提取部分:
word 7, word 8, word 9, word 14
得到:
word 7, word 8, word 9
word 14
使用正则表达式。我所做的是使用(word (?<num>'d+),?'s*)+
,然后检查每个捕获的数字。
是否有可能有一个正则表达式直接提取只有部分与连续的数字?
由于非regex解决方案是可接受的:
var data = "word 7, word 8, word 9, word 14";
// split the data into word and number
var dataCollection = data.Split(',').Select (d => new
{
word = d.Trim().Split(' ')[0],
number = int.Parse(d.Trim().Split(' ')[1])
}).ToList();
// store each set of consective results into a collection
List<string> resultsCollection = new List<string>();
var sb = new StringBuilder();
int i = 0;
while(i < dataCollection.Count ())
{
if(i > 0)
{
if(dataCollection[i].number == dataCollection[i-1].number + 1)
{
if(sb.Length > 0) sb.Append(", ");
}
else
{
resultsCollection.Add(sb.ToString());
sb.Clear();
}
}
sb.AppendFormat("{0} {1}", dataCollection[i].word, dataCollection[i].number);
i++;
}
resultsCollection.Add(sb.ToString());
对于您的测试数据,resultsCollection
将包含两个项目:
单词7,单词8,单词9
词14
仅使用正则表达式是不可能的,因为正则表达式只能描述正则语言。
常规语言,除其他限制外,不允许定义上下文,在您的情况下,上下文将是字符串中最新的met数。
有关语言和语法理论的更多信息,请参见乔姆斯基层次结构。
或者,您可以使用:
string words = "word 7, word 8, word 9, word 14";
string[] splittedWords = Regex.Split(words, ", "); //Separating words.
List<string> sortedWords = new List<string>();
int currentWordNumber = 0, lastWordNumber = 0;
foreach (string sptw in splittedWords)
{
if (sortedWords.Count == 0) //No value has been written to the list yet, so:
{
sortedWords.Add(sptw);
lastWordNumber = int.Parse(sptw.Split(' ')[1]); //Storing the number of the word for checking it later.
}
else
{
currentWordNumber = int.Parse(sptw.Split(' ')[1]);
if (currentWordNumber == lastWordNumber + 1)
sortedWords[sortedWords.Count - 1] += ", " + sptw;
else
sortedWords.Add(sptw);
lastWordNumber = currentWordNumber; //Storing the number of the word for checking it later.
}
}
最后,列表sortedWords将有:
"word 7, word 8, word 9"
"word 14"
LINQ对于所有类型的序列都非常方便。它有许多有用的操作符,但您也可以定义自己的操作符。你可以这样使用它:
"word 10, word 11, word 7, word 8, word 9, word 14, word 2"
.Split( new [] {", "}, StringSplitOptions.RemoveEmptyEntries)
.ToPartitionsOfConsecutiveValues(w => Int32.Parse(w.Split(' ').Last()))
.Select(sequence => String.Join(", ", sequence))
.ToArray()
.Dump("Array of strings");
Dump
来自LINQPad。
这是新的操作符:
public static class Partition {
public static IEnumerable<List<T>> ToPartitionsOfConsecutiveValues<T>(
this IEnumerable<T> source,
Func<T,int> valueSelector)
{
var lastValue = (int?)null;
List<T> lastList = null;
foreach (var item in source)
{
var value = valueSelector(item);
if (!(lastValue.HasValue))
{
lastList = new List<T>();
}
else if (lastValue.Value != value - 1)
{
yield return lastList;
lastList = new List<T>();
}
lastValue = value;
lastList.Add(item);
}
if (lastValue.HasValue) yield return lastList;
}
}
根据@ l.b.的评论更新。
LINQ操作符在具有尽可能少的具体类型时最有用。提取使用项目类型(int
)的谓词允许在其他情况下使用操作符。
下面是同样的例子:
Func<String,Int32> IntSuffix = w => Int32.Parse(w.Split(' ').Last());
Func<String, String, Boolean> breakPredicate
= (prev, next) => IntSuffix(prev) != IntSuffix(next) - 1;
s.Split( new [] {", "}, StringSplitOptions.RemoveEmptyEntries)
.ToPartitionsOfSequences(breakPredicate)
.Select (sequence => String.Join(", ", sequence))
实现:public static IEnumerable<List<T>> ToPartitionsOfSequences<T>(
this IEnumerable<T> source,
Func<T, T, Boolean> breakPredicate)
{
T lastItem = default(T);
List<T> lastList = null;
foreach (var item in source)
{
if (lastList == null)
{
lastList = new List<T>();
}
else if (breakPredicate(lastItem, item))
{
yield return lastList;
lastList = new List<T>();
}
lastItem = item;
lastList.Add(item);
}
if (lastList != null) yield return lastList;
}