Regular Expression for multiline textbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sabbysingh
    New Member
    • Feb 2010
    • 1

    #1

    Regular Expression for multiline textbox

    Hi All

    I have a regular expression
    ValidationExpre ssion="^((?!scr ipt).)*$"
    this works good when I am using it for a normal(singleli ne) textbox.
    But when i use this with Multiline textbox it does not work. I am using RegularExpressi onValidator control of asp.net for the same.
    Would there be any way I can make it compatible with Multiline textbox?

    Thanks In advance.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I'm bad with RegEx, but thanks to other articles on here I was able to build these two reusable functions a while back

    Code:
    // Function to return a string[] from a source string and Regex pather
            public static List<string> FindWithin(string ToSearch, string Pattern)
            {
                List<string> Results = new List<string>();
                Regex r = new Regex(Pattern);
                MatchCollection mc = r.Matches(ToSearch);
                foreach (Match m in mc)
                {
                    Results.Add(m.Value);
                }
                return Results;
            }
    
            /// <summary>
            /// Returns a list of all the strings between the StartMarker and EndMarker
            /// <example>"[123]abc[456]def[789]ghi", "[", "]" to find all the numbers between brackets</example>
            /// </summary>
            /// <param name="Source"></param>
            /// <param name="StartMarker"></param>
            /// <param name="EndMarker"></param>
            /// <returns></returns>
            public static List<string> FindBetween(string ToSearch, string StartMarker, string EndMarker)
            {
                string pattern = @"(?<=\" + StartMarker + @").+?(?=\" + EndMarker + ")";
                return FindWithin(ToSearch, pattern);
            }
    This way I don't have to remember how to build the RegEx, I just send the string to be searched, the start marker and the end marker to the 'FindBetween' method. It returns a List<string> of all the matches.
    Last edited by tlhintoq; Feb 17 '10, 02:48 PM. Reason: Better variable names to make more readable

    Comment

    Working...