Click here to Skip to main content
14,922,560 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
There is the string
-aaa-bbb-ccc-ddd-eee-fff-ggg-hhh-iii-jjj-kkk-lll-

I want to fetch string between "-" this special character using regex.

I've used this following code; here is the code snippet.
Regex r = new Regex(@"-(.+?)-");
MatchCollection mc = r.Matches(name);


But it's not working. Please help me to solve this.
Thanks in advance.
Posted
Comments
Pranav-BiTwiser 16-May-14 6:49am
   
y using regex only?
sahabiswarup 16-May-14 6:59am
   
what will you prefer?

From my point of view, using a regular expression is an engineering overkill. Fact is that regular expressions were designed for complex operations on strings, and getting the text between a separator (or "special character", as you call it) is not considered a complex operation. All of the major frameworks which (basically 99% of all languages) provide a string class also provide a method to split a string by a separator.
In C#, you can achieve what you want in the following way:

C#
using System;

public class SplitTest {
    public static void Main() {

        string yourString= "-aaa-bbb-ccc-ddd-eee-fff-ggg-hhh-iii-jjj-kkk-lll-"

        string [] split = words.Split(new Char [] {'-'}, StringSplitOptions.RemoveEmptyEntries);//set your list of separators, Empty entries ("") are removed

        foreach (string s in split) {
                Console.WriteLine(s);
        }
    }
}
// The example displays the following output to the console:
//       aaa
//       bbb
//       ccc
//       ddd
//       eee
//       fff
//       ggg
//       hhh
//       jjj
//       kkk
//       lll
   
Comments
Legor 16-May-14 7:37am
   
I agree that given the requirements do not change this may be an easier solution. Imo regex are easier to expand and are beter understandable for more complex tasks (you have to know regex of course).
Marco Bertschi 16-May-14 7:45am
   
I am with you - I assume that a solution like mine is changed to a regex-based solution as soon as changing requirements lead to a need of a more complex parsing routine.
Nevertheless I think one should go with the easiest possible way from the beginning on, a good architecture will changes as exchanging my solution with a regex easy.
sahabiswarup 16-May-14 7:39am
   
well, keep this in mind in future. Thanks for your suggestion.
Marco Bertschi 16-May-14 7:46am
   
You are welcome - Always go for the easiest possible solution, you can go for a more complex solution as soon as the need arises.
(?<=-)[a-zA-Z]+(?=-)
   
Comments
Marco Bertschi 16-May-14 7:52am
   
Peter,

congratulations, you managed to submit a solution that really works - Nevertheless I'd recommend the use of the string.split class (easier to implement and better understandable - You can always change it to use regex if the need arises), but have my 5* for the working solution anyways.
try @"-?(.+?)-" so the patern can overlap
   
Comments
sahabiswarup 16-May-14 6:56am
   
Thanks a lot.
You save me. \m/
Marco Bertschi 16-May-14 7:48am
   
While your solution works reasonably well, the output still contains the separators and not only the text between the separators. A cleaner way would be to use the string.split function.
try this
C#
Regex r = new Regex("-(.*)-");
MatchCollection mc = r.Matches(name);
   
Comments
sahabiswarup 16-May-14 6:58am
   
I've tried this, it is not as per my condition. Anyway thanks for your support.
Marco Bertschi 16-May-14 7:50am
   
This doesn't work very nice, a regex that returns a single match (with multiple groups) would be a lot easier to use. Even better would a solution work that uses the string.split function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900