tags:

views:

96

answers:

3

Hello everyone,

I want to capture word "assignment" only when it is found at the begining of line and line ends after that word.There can be zero or more space characters after "assignment" word and characters like : or # or - might come.

For example following lines should match

Assignments

or

Assignments :

or

assignments

Where as, following string should not match

The details of various assignments that I have ...

I get following line from one file which contains two occurances of "assignment" word.

Ab Initio\r\r\a\r\a\v\r\r\fAssignments\rThe details of the various assignments that I

I wrote following regular expression, but it is not able to capture anything :

^Assignments(\s|:|-|#)*?$

When I write regex like below, both the occurances of "assignment" get selected :

Assignments(\s|:|-|#)*?($)?

Any guesses? What should I do? I am using C# for this.

My C# code is as follows :

RegEx  x = new Regex(@"^Assignments(\s|:|-|#)*?$", RegexOptions.IgnoreCase | RegexOptions.Multiline);

output = x.Replace(inputText, "@@@@@@@@@@@@@@@@\r\n<project_details>$&");
            if (x.IsMatch(inputText))
            {
                Match m = x.Match(inputText);

                Console.WriteLine("\n\n\t~~~~~~~~~~   match found ~~~~~~~~~~~");
                Console.WriteLine(m.Index +" : " + m.Value);
                Console.WriteLine("\n\n\n\n" + output);                
            }
            else
            {
                Console.WriteLine("$$$$$$$$$$$$$ no match  %%%%%%%%%%%%%%");
            }

Just now I re checked my input string. Original lines in file is as follows :

Assignments
The details of various assignmenths that I ...

But when I load filestream into one string variable, I get same line like this :

\r\r\a\r\a\v\r\r\fAssignments\rThe details of the various assignments that I

Anyone knows what is happening? How should I formulate my regular expression? Please help !!!!

+1  A: 

Use RegexOptions.Multiline with your regex, this will change the meaning of ^ and $ to match start/end of line respectively (instead of match the start/end of the whole string).

reko_t
@Reko_t, Thanks for reply.I have alredy changed RegExOption as you have mentioned :My RegEx object is as follows :RegEx x = new Regex(@"^Assignments(\s|:|-|#)*?$", RegexOptions.IgnoreCase | RegexOptions.Multiline);Even then also I am not able to get the desired result.Any guess? Is there anything wrong in my regular expression?
Shekhar
+1  A: 

The first solution works if you use case insensitive and multiline:

Regex RegexObj = new Regex("^Assignments(\\s|:|-|#)*?$",
     RegexOptions.IgnoreCase | RegexOptions.Multiline);
Andrea Ambu
hi Andrea,I have set both the options, but then also I am not getting desired result.
Shekhar
+1  A: 

I'm going to go ahead and assume that you don't really need that form-feed character (\f) in there, and if not this will work:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ScratchConsole
{
    class Program
    {
        private static string[] punctuationChars = new string[] 
        {
            ":",
            ";"
        };
        static void Main(string[] args)
        {
            string foo = "Ab Initio\r\r\a\r\a\v\r\rAssignments\rThe details of the various assignments that I";
            string[] split = foo.Split(new string[] { "\r" },StringSplitOptions.None);
            foreach (string s in split)
            {
                if (s.StartsWith("Assignments"))
                {
                    string temp = s.Remove(0, "Assignments".Length );
                    foreach (string c in punctuationChars)
                    {
                        temp = temp.Replace(c, "");
                    }
                    if (string.IsNullOrEmpty(temp.Trim()))
                    {
                        Console.WriteLine("it worked!");
                    }
                }
            }
            Console.Read();
        }
    }
}

As i recall, there's a quote that goes something like "Sometimes, someone has a problem, and they decide to use regexes. Now they have two problems." (Not the actual quote but good enough :) )

RCIX
Thank RCIX for answer.Yes I do not need formfeed (\f) character but I have to use regular expression because the problem which I am solving is not related to this regex only, I am facing same problem with other regular expressions also. I have to use regex for my projec.
Shekhar
Oh. I'm sorry then, the most i can do is offer you a link to help with regexes, maybe that would help. http://www.regular-expressions.info/ or http://www.txt2re.com/
RCIX