Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Reguler expression [modified]

Reguler expression [modified]

Scheduled Pinned Locked Moved C#
regexhelp
8 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bhaskarsgb
    wrote on last edited by
    #1

    Hello All, I have problem with regular expression. i have one string can any tell the regular expression for that string. String text = AAAA!1234,BBBB;11111/CCCCC:2222/ In this string. I want 1)the string between ! and , as a "Jobid" means 1234 2)the string between ; and / as a "Field1" means 11111 3)the string between : and / as a "Field2" means 2222 Can any one tell the regular expression for that string. Thanks Bhaskar

    modified on Friday, April 16, 2010 6:31 AM

    R M 2 Replies Last reply
    0
    • B bhaskarsgb

      Hello All, I have problem with regular expression. i have one string can any tell the regular expression for that string. String text = AAAA!1234,BBBB;11111/CCCCC:2222/ In this string. I want 1)the string between ! and , as a "Jobid" means 1234 2)the string between ; and / as a "Field1" means 11111 3)the string between : and / as a "Field2" means 2222 Can any one tell the regular expression for that string. Thanks Bhaskar

      modified on Friday, April 16, 2010 6:31 AM

      R Offline
      R Offline
      riced
      wrote on last edited by
      #2

      Do you need a regular expression? Something like this might do.

      char[] splitChars = {'!', ',', ';', '/', ':'};
      string[] theBits = text.Split(splitChars);
      foreach(string s in theBits)
      {
      // do something with s
      }

      Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

      B 1 Reply Last reply
      0
      • R riced

        Do you need a regular expression? Something like this might do.

        char[] splitChars = {'!', ',', ';', '/', ':'};
        string[] theBits = text.Split(splitChars);
        foreach(string s in theBits)
        {
        // do something with s
        }

        Regards David R --------------------------------------------------------------- "Every program eventually becomes rococo, and then rubble." - Alan Perlis The only valid measurement of code quality: WTFs/minute.

        B Offline
        B Offline
        bhaskarsgb
        wrote on last edited by
        #3

        Thanks for ur reply, I need the regular expression like this, Regex rx = new Regex(@"(!(?<jobid>.*?),)(.*)(;(?<field1>.*?)/)(.*)(:(?<field2>.*?)/)(.*)",RegexOptions.Compiled | RegexOptions.IgnoreCase); in the above regular expression i am extracting the string based on the groups. but if one of the group is not present in the string. it is not matching. example "aaa!1234,BBBBCCCCC:222222/" if the text is like this the regular expression is not matching.

        G J 2 Replies Last reply
        0
        • B bhaskarsgb

          Thanks for ur reply, I need the regular expression like this, Regex rx = new Regex(@"(!(?<jobid>.*?),)(.*)(;(?<field1>.*?)/)(.*)(:(?<field2>.*?)/)(.*)",RegexOptions.Compiled | RegexOptions.IgnoreCase); in the above regular expression i am extracting the string based on the groups. but if one of the group is not present in the string. it is not matching. example "aaa!1234,BBBBCCCCC:222222/" if the text is like this the regular expression is not matching.

          G Offline
          G Offline
          Gideon Engelberth
          wrote on last edited by
          #4

          To make a group optional, simply append the ? character to the group. This should do the trick: @"(!(?<jobid>.*?),)?(.*)(;(?<field1>.*?)/)?(.*)(:(?<field2>.*?)/)?(.*)"

          1 Reply Last reply
          0
          • B bhaskarsgb

            Hello All, I have problem with regular expression. i have one string can any tell the regular expression for that string. String text = AAAA!1234,BBBB;11111/CCCCC:2222/ In this string. I want 1)the string between ! and , as a "Jobid" means 1234 2)the string between ; and / as a "Field1" means 11111 3)the string between : and / as a "Field2" means 2222 Can any one tell the regular expression for that string. Thanks Bhaskar

            modified on Friday, April 16, 2010 6:31 AM

            M Offline
            M Offline
            Morgs Morgan
            wrote on last edited by
            #5

            The best way you can ever sooth around validation expressions is to Try Me(Expresso)... Good luck. :^)

            1 Reply Last reply
            0
            • B bhaskarsgb

              Thanks for ur reply, I need the regular expression like this, Regex rx = new Regex(@"(!(?<jobid>.*?),)(.*)(;(?<field1>.*?)/)(.*)(:(?<field2>.*?)/)(.*)",RegexOptions.Compiled | RegexOptions.IgnoreCase); in the above regular expression i am extracting the string based on the groups. but if one of the group is not present in the string. it is not matching. example "aaa!1234,BBBBCCCCC:222222/" if the text is like this the regular expression is not matching.

              J Offline
              J Offline
              JTS
              wrote on last edited by
              #6

              You can use a tool like Espresso to build your regular expressions. The link is http://www.ultrapico.com/Expresso.htm[^] Here is the regex I came up with: (?:!(?<JobId>\w*)(?:.*?),(?:.*?))?(?:;(?<Field1>\w*)?/??(?:.*?))?:(?<Field2>\w*)??/
              Expresso will generate the following C# code for you:
              // using System.Text.RegularExpressions; /// <summary> /// Regular expression built for C# on: Fri, Apr 16, 2010, 07:52:19 AM /// Using Expresso Version: 3.0.3634, http://www.ultrapico.com /// /// A description of the regular expression: /// /// Match expression but don't capture it. [!(?<JobId>\w*)(?:.*?),(?:.*?)], zero or one repetitions /// !(?<JobId>\w*)(?:.*?),(?:.*?) /// ! /// [JobId]: A named capture group. [\w*] /// Alphanumeric, any number of repetitions /// Match expression but don't capture it. [.*?] /// Any character, any number of repetitions, as few as possible /// , /// Match expression but don't capture it. [.*?] /// Any character, any number of repetitions, as few as possible /// Match expression but don't capture it. [;(?<Field1>\w*)?/??(?:.*?)], zero or one repetitions /// ;(?<Field1>\w*)?/??(?:.*?) /// ; /// [Field1]: A named capture group. [\w*], zero or one repetitions /// Alphanumeric, any number of repetitions /// /, zero or one repetitions, as few as possible /// Match expression but don't capture it. [.*?] /// Any character, any number of repetitions, as few as possible /// : /// [Field2]: A named capture group. [\w*], zero or one repetitions, as few as possible /// Alphanumeric, any number of repetitions /// / /// /// /// </summary> public static Regex regex = new Regex( "(?:!(?<JobId>\\w*)(?:.*?),(?:.*?))?(?:;(?<Field1>\\w*)?/??(?"+ ":.*?))?:(?<Field2>\\w*)??/", RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.Compiled ); //// Replace the matched text in the InputText using the replacement pattern // string result = regex.Replace(InputText,regexReplace); //// Split the InputText wherever the regex matches // string[] results = regex.Split(InputText); //// Capture the fir

              OriginalGriffO B 2 Replies Last reply
              0
              • J JTS

                You can use a tool like Espresso to build your regular expressions. The link is http://www.ultrapico.com/Expresso.htm[^] Here is the regex I came up with: (?:!(?<JobId>\w*)(?:.*?),(?:.*?))?(?:;(?<Field1>\w*)?/??(?:.*?))?:(?<Field2>\w*)??/
                Expresso will generate the following C# code for you:
                // using System.Text.RegularExpressions; /// <summary> /// Regular expression built for C# on: Fri, Apr 16, 2010, 07:52:19 AM /// Using Expresso Version: 3.0.3634, http://www.ultrapico.com /// /// A description of the regular expression: /// /// Match expression but don't capture it. [!(?<JobId>\w*)(?:.*?),(?:.*?)], zero or one repetitions /// !(?<JobId>\w*)(?:.*?),(?:.*?) /// ! /// [JobId]: A named capture group. [\w*] /// Alphanumeric, any number of repetitions /// Match expression but don't capture it. [.*?] /// Any character, any number of repetitions, as few as possible /// , /// Match expression but don't capture it. [.*?] /// Any character, any number of repetitions, as few as possible /// Match expression but don't capture it. [;(?<Field1>\w*)?/??(?:.*?)], zero or one repetitions /// ;(?<Field1>\w*)?/??(?:.*?) /// ; /// [Field1]: A named capture group. [\w*], zero or one repetitions /// Alphanumeric, any number of repetitions /// /, zero or one repetitions, as few as possible /// Match expression but don't capture it. [.*?] /// Any character, any number of repetitions, as few as possible /// : /// [Field2]: A named capture group. [\w*], zero or one repetitions, as few as possible /// Alphanumeric, any number of repetitions /// / /// /// /// </summary> public static Regex regex = new Regex( "(?:!(?<JobId>\\w*)(?:.*?),(?:.*?))?(?:;(?<Field1>\\w*)?/??(?"+ ":.*?))?:(?<Field2>\\w*)??/", RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.Compiled ); //// Replace the matched text in the InputText using the replacement pattern // string result = regex.Replace(InputText,regexReplace); //// Split the InputText wherever the regex matches // string[] results = regex.Split(InputText); //// Capture the fir

                OriginalGriffO Offline
                OriginalGriffO Offline
                OriginalGriff
                wrote on last edited by
                #7

                5 from me! I have been using Expresso for a year or so, and had never spotted it could generate the C# code! Admittedly, it's not an onerous job, but the comment generation is very handy! Thanks a lot - Friday has not been wasted for me: I've learned something new... :-D

                You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                1 Reply Last reply
                0
                • J JTS

                  You can use a tool like Espresso to build your regular expressions. The link is http://www.ultrapico.com/Expresso.htm[^] Here is the regex I came up with: (?:!(?<JobId>\w*)(?:.*?),(?:.*?))?(?:;(?<Field1>\w*)?/??(?:.*?))?:(?<Field2>\w*)??/
                  Expresso will generate the following C# code for you:
                  // using System.Text.RegularExpressions; /// <summary> /// Regular expression built for C# on: Fri, Apr 16, 2010, 07:52:19 AM /// Using Expresso Version: 3.0.3634, http://www.ultrapico.com /// /// A description of the regular expression: /// /// Match expression but don't capture it. [!(?<JobId>\w*)(?:.*?),(?:.*?)], zero or one repetitions /// !(?<JobId>\w*)(?:.*?),(?:.*?) /// ! /// [JobId]: A named capture group. [\w*] /// Alphanumeric, any number of repetitions /// Match expression but don't capture it. [.*?] /// Any character, any number of repetitions, as few as possible /// , /// Match expression but don't capture it. [.*?] /// Any character, any number of repetitions, as few as possible /// Match expression but don't capture it. [;(?<Field1>\w*)?/??(?:.*?)], zero or one repetitions /// ;(?<Field1>\w*)?/??(?:.*?) /// ; /// [Field1]: A named capture group. [\w*], zero or one repetitions /// Alphanumeric, any number of repetitions /// /, zero or one repetitions, as few as possible /// Match expression but don't capture it. [.*?] /// Any character, any number of repetitions, as few as possible /// : /// [Field2]: A named capture group. [\w*], zero or one repetitions, as few as possible /// Alphanumeric, any number of repetitions /// / /// /// /// </summary> public static Regex regex = new Regex( "(?:!(?<JobId>\\w*)(?:.*?),(?:.*?))?(?:;(?<Field1>\\w*)?/??(?"+ ":.*?))?:(?<Field2>\\w*)??/", RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.Compiled ); //// Replace the matched text in the InputText using the replacement pattern // string result = regex.Replace(InputText,regexReplace); //// Split the InputText wherever the regex matches // string[] results = regex.Split(InputText); //// Capture the fir

                  B Offline
                  B Offline
                  bhaskarsgb
                  wrote on last edited by
                  #8

                  Hello JTS Thank u Very much, u helped me alot. once again thank u...

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups