Reguler expression [modified]
-
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
-
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
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.
-
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.
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.
-
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.
To make a group optional, simply append the ? character to the group. This should do the trick:
@"(!(?<jobid>.*?),)?(.*)(;(?<field1>.*?)/)?(.*)(:(?<field2>.*?)/)?(.*)"
-
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
The best way you can ever sooth around validation expressions is to Try Me(Expresso)... Good luck. :^)
-
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.
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
-
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
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
-
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
Hello JTS Thank u Very much, u helped me alot. once again thank u...