2

I am working in c# and i am trying to check a string for a value {some value}. Most importantly i am looking for whether the string contains {}, with something in between. I was thinking reg-ex but i don't really know how to implement that within a String.Contains. I have tried creating a reg-ex instance and passing that into the Contains but that doesnt seem to work. Please let me know if there is an easier approach.

if (uri.Contains("{one word}"))
    {

    }
return false;

This is what ive tried,

public bool SimpleResponseCodeCheck(string callTested, string testName) 
{
    testParams = SupportingMethodsClass.ReturnXMLData(callTested, testName);
    string uri;
    string method;
    string requestBody;
    string expResponseCode;
    string actResponseCode;
    string statusMessage;
    string respBody;
    Regex testParameter = new Regex("\\{.+\\}");

    testParams.TryGetValue("uri", out uri);
    testParams.TryGetValue("method", out method);
    testParams.TryGetValue("expResponseCode", out expResponseCode);
    testParams.TryGetValue("requestBody", out requestBody);

    if (testParameter.IsMatch(uri, 0))
    {

    }
    return false;
}
3
  • Why would you even try passing a Regex to Contains? Do you not have access to the documentation? Commented Dec 26, 2013 at 15:52
  • @JohnSaunders: The right documentation to look at is the docs for the Regex class. Commented Dec 26, 2013 at 16:34
  • @SLaks: I meant that the OP knows about Contains, and that a glance at the documentation for that method would have shown that it has no regular expression functionality. Commented Dec 26, 2013 at 17:45

3 Answers 3

5

You're looking for the Regex.IsMatch() function, which does exactly that.

Sign up to request clarification or add additional context in comments.

3 Comments

.IsMatch is exactly what i was looking for thanks, as mentioned below i used the RegEx expression "\\{.+\\}"
@hfrog713: In the future, if you see a class and can't figure out how to use it, read the documentation! msdn.microsoft.com/en-us/library/…
Ok i appreciate the advice, i guess what i was looking for help on was more the regular expression that i would use. Does mine appear correct?
1

If you are creating a Regex object with a pattern, you can use the Match() method on the object to check if it matches the pattern. With the Match result object, you can check the Success property to see if it matched or not.

Additionally, it may be useful to extract the word inside of the {...} bit, which can be easily done with named capture groups.

Here's a simple example:

        var stringA = "This contains a {parameter} somewhere!";
        var stringB = "This contains an {argument} instead.";
        var stringC = "No parameters or arguments here!";

        var simpleRegex = new Regex(@"{[a-zA-Z0-9_]+}");
        var groupRegex = new Regex(@"{(?<Parameter>[a-zA-Z0-9_]+)}");

        var simpleMatchA = simpleRegex.Match(stringA);
        var simpleMatchB = simpleRegex.Match(stringB);
        var simpleMatchC = simpleRegex.Match(stringC);

        var groupMatchA = groupRegex.Match(stringA);
        var groupMatchB = groupRegex.Match(stringB);
        var groupMatchC = groupRegex.Match(stringC);

        Console.WriteLine("String A: {0}", stringA);
        Console.WriteLine("String B: {0}", stringB);
        Console.WriteLine("String C: {0}", stringC);

        Console.WriteLine("Does A match? {0}", simpleMatchA.Success);
        Console.WriteLine("Does B match? {0}", simpleMatchB.Success);
        Console.WriteLine("Does C match? {0}", simpleMatchC.Success);

        Console.WriteLine("A value: {0}", groupMatchA.Groups["Parameter"].Value);
        Console.WriteLine("B value: {0}", groupMatchB.Groups["Parameter"].Value);
        Console.WriteLine("C value: {0}", groupMatchC.Groups["Parameter"].Value);

Here's the output:

String A: This contains a {parameter} somewhere!
String B: This contains an {argument} instead.
String C: No parameters or arguments here!
Does A match? True
Does B match? True
Does C match? False
A value: parameter
B value: argument
C value:

1 Comment

Thanks for the useful answer, gave me more information than i needed but that is never a bad thing. I ended up just using the .IsMatch method on a Regex class testParameter = new Regex("\\{.+\\}"); this seems to work aswell.
0

I believe you could try this:

Regex.Match(targetString, @"({)%s(})");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.