![](/img/trans.png)
[英]Is there a way I can find out if a string in C# does not contain the numbers & characters 0-9 and A-Z?
[英]How to extract first character and numbers after that until find a character (a-z) in a string - C# 2.0
如何從字符串的第二個索引中提取數字直到在字符串中找到字符(az)?
我使用的是 C# 2.0 版(由於某些原因無法升級)。
這里有些例子;
我試過 Regex("[^0-9]") 如果數字后沒有字符(第四個例子)
"B" + regexOnlyNumbers.Replace(mystring, string.Empty);
您可以使用
string text = "M000067TGFD45F";
Match m = Regex.Match(text, @"^[A-Z][0-9]+");
if (m.Success)
{
Console.WriteLine(m.Value);
}
請參閱正則表達式演示。
細節
^
- 字符串的開始[AZ]
- 一個大寫的 ASCII 字母[0-9]+
- 一個或多個 ASCII 數字。 或者,您可以考慮^[AZ][^AZ]+
模式,其中[^AZ]+
匹配除大寫 ASCII 字母之外的任何一個或多個字符。
要忽略大小寫,請使用RegexOptions.IgnoreCase
: Regex.Match(text, @"^[AZ][0-9]+", RegexOptions.IgnoreCase)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.