[英]Get string plus a certain amount of characters after from text
環顧四周后,我似乎無法找到我的問題的答案。
我有一段文字:“這是一些文本級別 190 這是一些更多的文本售價為 1999”
我需要獲得 190 值。 我只想提取所有數字,但大多數文本都不止一組數字。
我試圖對“級別 190”進行子字符串化,但最終還是留下了字符。 如何在“級別 190”之后刪除所有文本並僅提取該特定文本?
代碼示例:
string CIT = "ID 12321 ITEM name is ITEM level 100 cost some gold around 129"
string a = "level";
var index = CurrentItem.ToLower().IndexOf(a);
var final = index + 9; // Index of "level" is 28, add 4 for space and 3 numbers (level length + 4 = 9)
string CurrentItemSub = CurrentItem.Substring(index, final); // Sub it
Messagebox.Show(CurrentItemSub);
以下是獲取“level”值的兩種不同方法——假設單詞“level”在所需值之前。
給定以下用戶輸入: ID 12321 has level 100 and the cost is around 129.
選項 1 (使用正則表達式)
添加 using 語句:
using System.Text.RegularExpressions;
創建匹配
注意:
^
表示匹配必須從字符串的開頭開始; 在多行模式下,它必須從行首開始。$
表示匹配必須出現在字符串的末尾或字符串末尾的\\n 之前; 在多行模式下,它必須出現在行尾或行尾的 \\n 之前。.
匹配除 \\n 之外的任何單個字符。 要匹配文字句點字符(. 或 \.),必須在它前面加上轉義字符 (\\.)。*
匹配前一個元素 0 次或更多次+
匹配前一個元素 1 次或多次?
匹配前一個元素 0 或 1 次*?
匹配前一個元素 0 次或更多次,但盡可能少+?
匹配前一個元素一次或多次,但盡可能少我們可以使用以下使用命名組的模式:
^.+level\\\\s+(?<level>\\\\d+).+$
注意:可能還有其他可以編寫的模式也會產生所需的數據。
^
表示從字符串(或行)的開頭開始.+
表示除\\n 之外的任何字符都應該匹配1 次或多次
level
匹配單詞“level”
\\\\s+
匹配 1 個或多個空格
命名組的格式是(?<nameOfGroup>patternToMatch)
。 因此(?<level>\\\\d+)
表示匹配 1 個或多個數字並將其放在名為“level”的組中。
.+
表示除 \\n 之外的任何字符都應該匹配 1 次或多次
$
表示匹配必須出現在字符串的末尾或字符串末尾的\\n 之前; 在多行模式下,它必須出現在行尾或行尾的 \\n 之前。
Match match = Regex.Match(userInput, "^.+level\\s+(?<level>\\d+).+$", RegexOptions.IgnoreCase);
檢查是否有任何匹配項並對結果進行處理:
if (match.Success && match.Groups.Count > 1)
{
for (int i = 0; i < match.Groups.Count; i++)
{
Group group = match.Groups[i];
System.Diagnostics.Debug.WriteLine("group [" + i + "]: Name: '" + group.Name + "' Value: " + group.Value);
}
System.Diagnostics.Debug.WriteLine("Level: '" + match.Groups["level"].ToString() + "'");
}
這是實現上述內容的方法:
獲取級別正則表達式:
private string GetLevelRegex(string userInput)
{
string level = string.Empty;
Match match = Regex.Match(userInput, "^.+level\\s+(?<level>\\d+).+$", RegexOptions.IgnoreCase);
if (match.Success && match.Groups.Count > 1)
{
for (int i = 0; i < match.Groups.Count; i++)
{
Group group = match.Groups[i];
System.Diagnostics.Debug.WriteLine("group [" + i + "]: Name: '" + group.Name + "' Value: " + group.Value);
}
level = match.Groups["level"].ToString();
//System.Diagnostics.Debug.WriteLine("Level: '" + level + "'");
}
return level;
}
選項 2 (無正則表達式)
聲明一個變量:
string level = string.Empty;
確保用戶輸入不為 null 或為空,並包含“級別”一詞:
if (!String.IsNullOrEmpty(userInput) && userInput.IndexOf("level", StringComparison.OrdinalIgnoreCase) >= 0)
{
}
用單個空格替換多個空格
string tempInput = userInput.Replace(@"\s+", " ");
獲取從“level”這個詞開始的子串; 刪除空間
level = userInput.Substring(userInput.IndexOf("level", StringComparison.OrdinalIgnoreCase) + 5).TrimStart();
這導致以下字符串: 100 and the cost is around 129.
現在,所需的值位於字符串的開頭,並在出現空格時結束。 獲取所需的值:
level = level.Substring(0, level.IndexOf(" "));
“level”現在包含以下字符串: 100
這是實現上述內容的方法:
獲取級別:
private string GetLevel(string userInput)
{
string level = string.Empty;
//ensure user input isn't null or empty AND user input contains the word "level"
if (!String.IsNullOrEmpty(userInput) && userInput.IndexOf("level", StringComparison.OrdinalIgnoreCase) >= 0)
{
//replace multiple spaces with single space
string tempInput = userInput.Replace(@"\s+", " ");
//get substring starting after the word "level"; remove space
level = userInput.Substring(userInput.IndexOf("level", StringComparison.OrdinalIgnoreCase) + 5).TrimStart();
//get text until a space is encountered
level = level.Substring(0, level.IndexOf(" "));
System.Diagnostics.Debug.WriteLine("level: '" + level + "'");
}
return level;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.