tags:

views:

161

answers:

4

I want to match the pattern: Starts with 0 or more spaces, followed by "ABC", then followed by anything.

So things like " ABC " " ABC111111" "ABC" would be matched.
But things like " AABC" "SABC" would not be matched.

I tried:

String Pattern = "^\\s*ABC(.*)";

But it does not work.

Any ideas? This is in C# by the way.

+2  A: 

Try

string pattern = @"\s*ABC(.*)"; // Using @ makes it easier to read regex.

I verified that this works on regexpl.com

SolutionYogi
Doesn't the ^ just force examination from the beginning of the line?
John Pirie
The ^ means the pattern starts at the beginning of the line.
Kleinux
A: 

Be sure you have set the regex engine to use SingleLine and not MultiLine.

Alan McBee
+1  A: 

The \\ usually puts in a literal backslash so that's probably where your solution is failing. Unless you are doing a replace you don't need the parentheses around the .*

Also \s matches characters besides the space character [ \t\n\f\r\x0B] or space, tab, newline, formfeed, return, and Vertical Tab.

I would suggest:

String Pattern = @"^[ ]*ABC.*$";  
Mark Thalman
C# requires you to escape the back-slashes in regexes like that.
Tom
What does adding @ in front of the pattern do?
Saobi
@Saobi - A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. From http://msdn.microsoft.com/en-us/library/aa691090(VS.71).aspx
JeffH
Hmm I see. And what does $ mean in your regex?
Saobi
Saobi, The $ matches until the end of the line. It should do that anyway, but I like to be explicit. The $ does not capture any line ending characters.
Mark Thalman
@Mark, since the OP's regex is *not* in a verbatim string, the backslash in \s has to be escaped with another backslash; it is not matching a literal backslash. (BTW, you need to enclose the double-backslash in your answer in `backticks` to keep SO from swallowing one of them.)
Alan Moore
Alan, I do not have any backslash characters in my answer. The character class is just a space. This is not necessary but I thought it would be clearer this way.
Mark Thalman
I meant in the text part of the answer, not the solution itself. Here, I'll get it...
Alan Moore
Alan, Thanks for the edit. I thought that had rendered correctly.
Mark Thalman
+1  A: 

I tested this. It works. You can omit RegexOptions.IgnoreCase if you want only upper case ABC to be matched.

/// <summary>
/// Gets the part of the string after ABC
/// </summary>
/// <param name="input">Input string</param>
/// <param name="output">Contains the string after ABC</param>
/// <returns>true if success, false otherwise</returns>
public static bool TryGetStringAfterABC(string input, out string output)
{
    output = null;

    string pattern = "^\\s*ABC(?<rest>.*)";

    if (Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase))
    {
        Regex r = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Compiled);
        output = r.Match(input).Result("${rest}");
        return true;
    }
    else
        return false;
}

Calling code:

static void Main(string[] args)
{
    string input = Console.ReadLine();

    while (input != "Q")
    {
        string output;
        if (MyRegEx.TryGetStringAfterABC(input, out output))
            Console.WriteLine("Output: " + output);
        else
            Console.WriteLine("No match");
        input = Console.ReadLine();
    }
}
Rashmi Pandit