Check for string absence in regex

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • varunhome@gmail.com

    Check for string absence in regex

    Hi,
    I want to check for the absence of a string in regular expression. For
    example, if the string is "Error opening file: Permission denied.
    Aborting.", I want to check for absence of the string "Permission ". I
    have tried many possibilities without success.

    Thanks for any help,

    Varun Sud

  • Rahul Anand

    #2
    RE: Check for string absence in regex

    "varunhome@gmai l.com" wrote:
    [color=blue]
    > Hi,
    > I want to check for the absence of a string in regular expression. For
    > example, if the string is "Error opening file: Permission denied.
    > Aborting.", I want to check for absence of the string "Permission ". I
    > have tried many possibilities without success.
    >
    > Thanks for any help,
    >
    > Varun Sud
    >[/color]

    Hi Varun,

    You can achieve the same using the code snippet below. You can choose either
    string basic operation or regex match.

    <CODE>

    string str = "Error opening file: Permission denied. Aborting.";
    string pattern = "Permission ";

    // Using RegEx methods
    Regex reg = new Regex(pattern);
    if(! reg.IsMatch(str ))
    Console.WriteLi ne("Not Found (RegEx operation)");
    else
    Console.WriteLi ne("Found Match (RegEx operation)");

    // Using string methods
    if(str.IndexOf( pattern) < 0)
    Console.WriteLi ne("Not Found (string operation)");
    else
    Console.WriteLi ne("Found Match (string operation)");

    </CODE>

    Hope it will help.

    --
    Cheers,
    Rahul Anand

    Comment

    Working...