sampath meka

sampath meka

  • NA
  • 41
  • 12.3k

Regular Expressions

Mar 12 2014 5:03 AM
Hello,
           Kindly help me i am stuck in the following expression...
 
string strTest = " (!(2VH_AKTIVLENKUNG_UND_HSR) + !(ALLRAD) + (B47D20 + G011, B47D20 + G012, B48B20 + G011, B48B20 + G012, B57D30 + G011, B57D30 + G012, B58B30 + G011, B58B30 + G012, G011 + N63B40, G012 + N63B40,)";
 
             string r = @"(?((?=.*\)))(?<=\)), |, )";
                foreach(string s in Regex.Split(strTest, r))
                {
                   Console.WriteLine(s);
}
 
 
I am using the above expression kindly correct to make the above expression work
 
Expected Output:
2VH_AKTIVLENKUNG_UND_HSR
 ALLRAD 
B47D20 + G011 
B47D20 + G012
B48B20 + G011
B48B20 + G012
B57D30+  G011
B57D30+ G012
B58B30 + G011
B58B30 + G012
G011 + N63B40
G012 + N63B40
 
 
 
 
 
 
 

Answers (5)

0
Vulpes

Vulpes

  • 0
  • 96k
  • 2.6m
Mar 24 2014 12:58 PM
I don't think there's any way that you can come up with a single regular expression which deals with both str1 and str2 and produces the desired output.

str2 is particularly difficult as the two last strings you want aren't even sub-strings of str2. They will therefore have to be manufactured from captured groups or in some other 'ad hoc' way.

I can try and come up with something that deals with str2 if you like but I thought I'd better warn you first that it won't be able to deal with str1 as well.

Software does exist which can generate regular expressions. Check out this thread for example:

http://stackoverflow.com/questions/910220/any-good-regular-expression-creator-software-or-online-tools-to-create-regular-e

but, frankly, I'll be very surprised if you can generate something here as you can't really define a pattern which the software can work from.

My own view is that it's better to learn the principles of regex and come up with your own expressions as at least you'll understand what you've done and be able to tweak it to deal with variations in the pattern.




0
sampath meka

sampath meka

  • 0
  • 41
  • 12.3k
Mar 24 2014 3:26 AM
Hello,


 Kindly plz help me

I want to modify this expression 

string regExp = @"\w+( \+ \w+)*";

To get above string expected output kindly make changes for above expression 

string str1= " (!(2VH_AKTIVLENKUNG_UND_HSR) + !(ALLRAD) + (B47D20 + G011, B47D20 + G012, B48B20 + G011, B48B20 + G012, B57D30 + G011, B57D30 + G012, B58B30 + G011, B58B30 + G012, G011 + N63B40, G012 + N63B40,)";

Expected Output:
!(2VH_AKTIVLENKUNG_UND_HSR)
!(ALLRAD)
B47D20 + G011 
B47D20 + G012
B48B20 + G011
B48B20 + G012
B57D30+  G011
B57D30+ G012
B58B30 + G011
B58B30 + G012
G011 + N63B40
G012 + N63B40

string str2 ="((ALLRAD + !(812_ENGLAND, US)), (ALLRAD + (812_ENGLAND, US)))";

Expected Output:

ALLRAD +!(812_ENGLAND, US))
ALLRAD +812_ENGLAND
ALLRAD +US


Kindly help me how can i modify the expression  regExp = @"\w+( \+ \w+)*";

which works for both str1 and str2 strings.

Thank u plz help me....


One more thing Is there any software which generates the expression based on string

Thanks
0
Vulpes

Vulpes

  • 0
  • 96k
  • 2.6m
Mar 23 2014 10:07 AM
No problem, try this:

string r = @"(!\(\w+\)|\w+\s\+\s\w+)";
MatchCollection mc = Regex.Matches(strTest, r);
foreach(Match m in mc) Console.WriteLine(m.Value);

The output should be:

!(2VH_AKTIVLENKUNG_UND_HSR)
!(ALLRAD)
B47D20 + G011
B47D20 + G012
B48B20 + G011
B48B20 + G012
B57D30 + G011
B57D30 + G012
B58B30 + G011
B58B30 + G012
G011 + N63B40
G012 + N63B40



0
sampath meka

sampath meka

  • 0
  • 41
  • 12.3k
Mar 23 2014 6:08 AM
Hi ,


       Sorry i typed my expected output wrong so plz correct the expression
string strTest = " (!(2VH_AKTIVLENKUNG_UND_HSR) + !(ALLRAD) + (B47D20 + G011, B47D20
+ G012, B48B20 + G011, B48B20 + G012, B57D30 + G011, B57D30 + G012, B58B30 + G011,
B58B30 + G012, G011 + N63B40, G012 + N63B40,)";

string r = @"(?((?=.*\)))(?<=\)), |, )";
                foreach(string s in Regex.Split(strTest, r))
                {
                        Console.WriteLine(s);
                }



Expected Output:

!(2VH_AKTIVLENKUNG_UND_HSR)

!(ALLRAD)
B47D20 + G011
B47D20 + G012
B48B20 + G011
B48B20 + G012
B57D30+  G011
B57D30+  G012
B58B30+ G011
B58B30+ G012
G011 + N63B40
G012 + N63B40


0
Vulpes

Vulpes

  • 0
  • 96k
  • 2.6m
Mar 12 2014 7:06 AM
Try instead:

string r = @"\w+( \+ \w+)*";
MatchCollection mc = Regex.Matches(strTest, r);
foreach(Match m in mc) Console.WriteLine(m.Value);