正则表达式子字符串通配符
本文关键字:通配符 字符串 正则表达式 | 更新日期: 2023-09-27 18:08:59
在c#中,我试图搜索子字符串"flight%sin",其中%s将是一个字符串。如何在c#中使用正则表达式?
您可以使用@"flight('w+)in"
捕获"flight"answers"in"之间的文本
参考指南提供了更多细节。
下面是一个c#中的正则表达式示例。
string [] mystrings = new string [] {"flight%sin", "flightTest1sin", "flighNoGoodsin", "flightTest2sin"};
foreach (string s in mystrings)
{
var groups = Regex.Match(s, @"flight('w+)in");
if (groups.Groups.Count > 1)
{
Console.WriteLine(groups.Groups[1]);
}
}
Console.ReadKey();
这有帮助吗?
string data = "flight 4057 in"; //I am guessing, this is what the original string will be.
public string getFlightNumber(string data)
{
int flightNumLength = 4;// Or however long the string would be
for(int index = 0; index < data.length(); index++)
{
if(index+flightNumLength + index < data.length())
{
string TempFlight = data.subSting(index, flightNumLength);
if(isNumeric(TempFlight))
{
return TempFlight;
}
}
}
}
public static bool IsNumeric(object Expression){
bool isNum;
double retNum;
isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any,System.Globalization.NumberFormatInfo.InvariantInfo, out retNum );
return isNum;
}