locked
Need a regular expression RRS feed

  • Question

  • I am in need of a regular expression which searches for a string pattern in an input string.The pattern is like this - a string that starts with = and then any text and then _CRIC and then any text.

    <anytext>=<anytext>_CRIC<anytext>

    Can anybody please provide me a regular expression for this? It's very very urgent. I am programming in C#.

    Thank you,
    Vidyashree


    • Edited by Vidya Bhatt Thursday, September 13, 2012 7:44 AM
    Thursday, September 13, 2012 7:43 AM

Answers

  • I think you wanted something like this:
    \b(_\w+|[\w-[0-9_]]\w*)\b([^\S\r\n]*)=([^\S\r\n]*)((\".+?_CRIC.+?\"))

    Sample:

    string input = "rate = \"Eric 2M Free_CRIC EGL\";\r\nsome other text\r\nx = \"test\"\r\nrate = \"Clip 4S Paid_CRIC EGL\";";
    MatchCollection matches = Regex.Matches(input, "\\b(_\\w+|[\\w-[0-9_]]\\w*)\\b([^\\S\\r\\n]*)=([^\\S\\r\\n]*)((\\\".+?_CRIC.+?\\\"))");
    foreach (Match match in matches)
    {
        MessageBox.Show(match.Value);
        //Console.WriteLine(match.Value);
    }

    • Proposed as answer by ardaoktay Thursday, September 13, 2012 10:58 AM
    • Marked as answer by Vidya Bhatt Thursday, September 13, 2012 12:22 PM
    Thursday, September 13, 2012 10:57 AM
  • Here is the regex:

    =.*?_CRIC.*?"

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Marked as answer by Vidya Bhatt Thursday, September 13, 2012 12:22 PM
    Thursday, September 13, 2012 10:29 AM
  • The problem with Regex is there is a hundred ways to do things correctly and a thousand ways to do it wrong... the only way you will get the regex you want is to build it yourself.

    Fortunately Regex is not too difficult - here is a great cheat sheet for you so you can quickly build your own regex expressions that do what you want the way you want.

    http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/


    Digital Forensic Software Developer
    CCS LABS Digital Forensic Software
    Mark as Answer or Vote up if useful thank you!

    • Proposed as answer by Dave A Gordon Thursday, September 13, 2012 11:08 AM
    • Marked as answer by Vidya Bhatt Thursday, September 13, 2012 12:22 PM
    Thursday, September 13, 2012 11:06 AM
  • "(\\w+\\s*)=(\\s*)\"(\\s*\\w\\s*)+\\w_CD(\\s*\\w*\\s*\")"

    Try this.


    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by Vidya Bhatt Thursday, September 13, 2012 12:22 PM
    Thursday, September 13, 2012 12:19 PM

All replies

  • Try:

    ^(.*=\s*)(.*)(_CRIC.*)$

    Your "text" is the 2nd match.

    "^" means the line starts

    ".*" means anything.

    "=" means =.

    "\s*" means 0 or more white spaces.

    "$" means the line ends


    I always try to help ;) sometimes I don't know how :(

    Thursday, September 13, 2012 7:48 AM
  • Thank you very much. I am sorry that I missed one more requirement. The string that follows = should be inside double quotes.

    Below are example inputs.

    rate = "Eric 2M Free_CRIC EGL"
    rate = "Clip 4S Paid_CRIC EGL"

    So the pattern is <anytext>= "<anytext>_CRIC<anytext>"

    Thursday, September 13, 2012 7:56 AM
  • Here is the regex:

    =.*?_CRIC.*?"

     

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Marked as answer by Vidya Bhatt Thursday, September 13, 2012 12:22 PM
    Thursday, September 13, 2012 10:29 AM
  • I think you wanted something like this:
    \b(_\w+|[\w-[0-9_]]\w*)\b([^\S\r\n]*)=([^\S\r\n]*)((\".+?_CRIC.+?\"))

    Sample:

    string input = "rate = \"Eric 2M Free_CRIC EGL\";\r\nsome other text\r\nx = \"test\"\r\nrate = \"Clip 4S Paid_CRIC EGL\";";
    MatchCollection matches = Regex.Matches(input, "\\b(_\\w+|[\\w-[0-9_]]\\w*)\\b([^\\S\\r\\n]*)=([^\\S\\r\\n]*)((\\\".+?_CRIC.+?\\\"))");
    foreach (Match match in matches)
    {
        MessageBox.Show(match.Value);
        //Console.WriteLine(match.Value);
    }

    • Proposed as answer by ardaoktay Thursday, September 13, 2012 10:58 AM
    • Marked as answer by Vidya Bhatt Thursday, September 13, 2012 12:22 PM
    Thursday, September 13, 2012 10:57 AM
  • The problem with Regex is there is a hundred ways to do things correctly and a thousand ways to do it wrong... the only way you will get the regex you want is to build it yourself.

    Fortunately Regex is not too difficult - here is a great cheat sheet for you so you can quickly build your own regex expressions that do what you want the way you want.

    http://www.cheatography.com/davechild/cheat-sheets/regular-expressions/


    Digital Forensic Software Developer
    CCS LABS Digital Forensic Software
    Mark as Answer or Vote up if useful thank you!

    • Proposed as answer by Dave A Gordon Thursday, September 13, 2012 11:08 AM
    • Marked as answer by Vidya Bhatt Thursday, September 13, 2012 12:22 PM
    Thursday, September 13, 2012 11:06 AM
  • "(\\w+\\s*)=(\\s*)\"(\\s*\\w\\s*)+\\w_CD(\\s*\\w*\\s*\")"

    Try this.


    Please mark this post as answer if it solved your problem. Happy Programming!

    • Marked as answer by Vidya Bhatt Thursday, September 13, 2012 12:22 PM
    Thursday, September 13, 2012 12:19 PM
  • Thank you all. All these answers give different solutions to the same problem. Thanks.
    Thursday, September 13, 2012 12:23 PM