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?
    }
}

      

+3


source to share


5 answers


How about a regular expression?



bool contains = Regex.IsMatch(m.text, @"\bMaterial\b");

      

+8


source


Contains a search for the exact word that you put as a parameter check this as an example in a small program

string a = "This is a test";
string b = "This is a TEST";
Console.WriteLine(a.Contains("test"));
Console.WriteLine(b.Contains("test"));

      



Hope I understood your problem well.

0


source


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
        }
    }
}

      

0


source


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

0


source


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);

      

0


source







All Articles