How to accurately match a word with C # contains a function?
I am trying to read scripts through C # and determine if they contain certain words, but those words must be identical, not just what I am looking for. Is there a way to use the contains
-function, highlight the word and check if it matches the exact word?
How do you determine if it contains and what matches a search term?
I am currently using the following script:
// GetScriptAssetsOfType<MonoBehaviour>() Returns all monobehaviour scripts as text
foreach (MonoScript m in GetScriptAssetsOfType<MonoBehaviour>())
{
if (m.text.Contains("Material"))
{
// Identical check?
}
}
source to share
Use another check inside. Hope I understood your problem.
// GetScriptAssetsOfType<MonoBehaviour>() Returns all monobehaviour scripts as text
foreach (MonoScript m in GetScriptAssetsOfType<MonoBehaviour>())
{
if (m.text.Contains("Material"))
{
if(m.text == "Material")
{
//Do Something
}
}
}
source to share
just using an extension method to make the Regex Handy
Extension class
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApplication11
{
public static class Extension
{
public static Match RegexMatch(this string input, string pattern, RegexOptions regexOptions = RegexOptions.IgnoreCase)
{
return Regex.Match(input, pattern, regexOptions);
}
}
}
and using the above class.
using System.Text;
namespace ConsoleApplication11
{
class Program
{
static void Main(string[] args)
{
bool isMatch = "this is text ".RegexMatch(@"\bis\b").Success;
}
}
}
see extension methods if not familiar http://www.codeproject.com/Articles/573095/A-Beginners-Tutorial-on-Extension-Methods-Named-Pa
source to share
So you are looking for regexp instead of Contains, now I understand your problem.
string a = "This is a test for you";
string b = "This is a testForYou";
string c = "test This is a for you";
string d = "for you is this test.";
string e = "for you is this test, and works?";
var regexp = new Regex(@"(\stest[\s,\.])|(^test[\s,\.])");
Console.WriteLine(regexp.Match(a).Success);
Console.WriteLine(regexp.Match(b).Success);
Console.WriteLine(regexp.Match(c).Success);
Console.WriteLine(regexp.Match(d).Success);
Console.WriteLine(regexp.Match(e).Success);
source to share