I am seeking a way to search a string for an exact match or whole word match. RegEx.Match
and RegEx.IsMatch
don't seem to get me where I want to be.
Consider the following scenario:
我正在寻找一种方法来搜索字符串以获得完全匹配或完整的单词匹配。 RegEx.Match和RegEx.IsMatch似乎没有让我到达我想要的地方。请考虑以下情形:
namespace test
{
class Program
{
static void Main(string[] args)
{
string str = "SUBTOTAL 34.37 TAX TOTAL 37.43";
int indx = str.IndexOf("TOTAL");
string amount = str.Substring(indx + "TOTAL".Length, 10);
string strAmount = Regex.Replace(amount, "[^.0-9]", "");
Console.WriteLine(strAmount);
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
}
}
The output of the above code is:
上面代码的输出是:
// 34.37
// Press any key to continue...
The problem is, I don't want SUBTOTAL
, but IndexOf
finds the first occurrence of the word TOTAL
which is in SUBTOTAL
which then yields the incorrect value of 34.37.
问题是,我不想要SUBTOTAL,但是IndexOf找到第一次出现的单词TOTAL,它在SUBTOTAL中,然后产生不正确的值34.37。
So the question is, is there a way to force IndexOf
to find only an exact match or is there another way to force that exact whole word match so that I can find the index of that exact match and then perform some useful function with it. RegEx.IsMatch
and RegEx.Match
are, as far as I can tell, simply boolean
searches. In this case, it isn't enough to just know the exact match exists. I need to know where it exists in the string.
所以问题是,有没有办法强制IndexOf只找到一个完全匹配或是否有另一种方法来强制完全匹配整个单词,以便我可以找到该完全匹配的索引,然后用它执行一些有用的功能。据我所知,RegEx.IsMatch和RegEx.Match只是布尔搜索。在这种情况下,仅知道存在完全匹配是不够的。我需要知道它在字符串中的位置。
Any advice would be appreciated.
任何意见,将不胜感激。
4 个解决方案
#1
9
You can use Regex
你可以使用Regex
string str = "SUBTOTAL 34.37 TAX TOTAL 37.43";
var indx = Regex.Match(str, @"\WTOTAL\W").Index; // will be 18
#2
0
While this may be a hack that just works for only your example, try
虽然这可能是一个仅适用于您的示例的黑客,但请尝试
string amount = str.Substring(indx + " TOTAL".Length, 10);
giving an extra space before total. As this will not occur with SUBTOTAL
, it should skip over the word you don't want and just look for an isolated TOTAL
.
在总计之前给予额外的空间。因为SUBTOTAL不会发生这种情况,所以它应该跳过你不想要的单词,只需找一个孤立的TOTAL。
#3
0
I'd recommend the Regex solution from L.B. too, but if you can't use Regex, then you could use String.LastIndexOf("TOTAL"). Assuming the TOTAL always comes after SUBTOTAL?
我推荐L.B.的Regex解决方案。也是如果你不能使用正则表达式,那么你可以使用String.LastIndexOf(“TOTAL”)。假设TOTAL总是在SUBTOTAL之后出现?
http://msdn.microsoft.com/en-us/library/system.string.lastindexof(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.string.lastindexof(v=vs.110).aspx
#4
0
My method is faster than the accepted answer because it does not use Regex.
我的方法比接受的答案更快,因为它不使用正则表达式。
string str = "SUBTOTAL 34.37 TAX TOTAL 37.43";
var indx = str.IndexOfWholeWord("TOTAL");
public static int IndexOfWholeWord(this string str, string word)
{
for (int j = 0; j < str.Length &&
(j = str.IndexOf(word, j, StringComparison.Ordinal)) >= 0; j++)
if ((j == 0 || !char.IsLetterOrDigit(str, j - 1)) &&
(j + word.Length == str.Length || !char.IsLetterOrDigit(str, j + word.Length)))
return j;
return -1;
}
#1
9
You can use Regex
你可以使用Regex
string str = "SUBTOTAL 34.37 TAX TOTAL 37.43";
var indx = Regex.Match(str, @"\WTOTAL\W").Index; // will be 18
#2
0
While this may be a hack that just works for only your example, try
虽然这可能是一个仅适用于您的示例的黑客,但请尝试
string amount = str.Substring(indx + " TOTAL".Length, 10);
giving an extra space before total. As this will not occur with SUBTOTAL
, it should skip over the word you don't want and just look for an isolated TOTAL
.
在总计之前给予额外的空间。因为SUBTOTAL不会发生这种情况,所以它应该跳过你不想要的单词,只需找一个孤立的TOTAL。
#3
0
I'd recommend the Regex solution from L.B. too, but if you can't use Regex, then you could use String.LastIndexOf("TOTAL"). Assuming the TOTAL always comes after SUBTOTAL?
我推荐L.B.的Regex解决方案。也是如果你不能使用正则表达式,那么你可以使用String.LastIndexOf(“TOTAL”)。假设TOTAL总是在SUBTOTAL之后出现?
http://msdn.microsoft.com/en-us/library/system.string.lastindexof(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.string.lastindexof(v=vs.110).aspx
#4
0
My method is faster than the accepted answer because it does not use Regex.
我的方法比接受的答案更快,因为它不使用正则表达式。
string str = "SUBTOTAL 34.37 TAX TOTAL 37.43";
var indx = str.IndexOfWholeWord("TOTAL");
public static int IndexOfWholeWord(this string str, string word)
{
for (int j = 0; j < str.Length &&
(j = str.IndexOf(word, j, StringComparison.Ordinal)) >= 0; j++)
if ((j == 0 || !char.IsLetterOrDigit(str, j - 1)) &&
(j + word.Length == str.Length || !char.IsLetterOrDigit(str, j + word.Length)))
return j;
return -1;
}