![](/img/trans.png)
[英]Regex pattern for checking if a string starts with ], a letter and a number
[英]Grabbing numbers from a string if it starts with a certain letter
我希望這里有人可以幫助我。 如果我有一個以字母“S”開頭的字符串,后跟一個長達 8 位的數字,之后是否可以獲取這些數字?
所以假設我有一個字符串“blah blah blah blah S124 blah”,我怎么能把 124 放在一個 int 變量中?
您可以使用正則表達式 (Regex) 來做到這一點。
在這種情況下,它將類似於:
// Matches letter S and numbers from 1 up to 8 times and encapsulates numbers in group
S(\d{1,8})
您可以從匹配中提取以獲取數字。
Regex numberRegex = Regex(regexString);
Match m = numberRegex.Match(stringToMatch);
String onlyNumbers = m.Groups[1].Value;
之后將其轉換為數字
int number = int.Parse(onlyNumbers)
你可以試試這個來避免組
Regex.Match(input,@"(?<=S)\d{1,8}\b").Value;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.