-2

I need to generate a regex to match any string with this structure:

{"anyWord"}{"aSpace"}{"-"}{"anyLetter"}

How can I do it?

Thanks

EDIT

I have tried:

  string txt="print -c";

  string re1="((?:[a-z][a-z]+))";   // Word 1

  Regex r = new Regex(re1,RegexOptions.IgnoreCase|RegexOptions.Singleline);
  Match m = r.Match(txt);
  if (m.Success)
  {
        String word1=m.Groups[1].ToString();
        Console.Write("("+word1.ToString()+")"+"\n");
  }
  Console.ReadLine();

but this only matches the word "print"

Cris
  • 2,002
  • 4
  • 30
  • 51

1 Answers1

4

This would be pretty straight-forward :

[a-zA-Z]+\s\-[a-zA-Z]

explained as follows :

[a-zA-Z]+        # Matches 1 or more letters
\s               # Matches a single space
\-               # Matches a single hyphen / dash
[a-zA-Z]         # Matches a single letter

If you needed to implement this in C#, you could just use the Regex class and specifically the Regex.Matches() method:

var matches = Regex.Matches(yourString,@"[a-zA-Z]+\s\-[a-zA-Z]");

Some example matching might look like this :

enter image description here

Rion Williams
  • 74,820
  • 37
  • 200
  • 327
  • 1
    Looking at the OP's name I can imagine that he is interested in this question: http://stackoverflow.com/questions/656157/regular-expression-to-catch-letters-beyond-a-z. Because he may want to match strings like "Benoîs-à". – Peter - Reinstate Monica Apr 07 '16 at 13:52
  • 2
    @AgapwIesu Just because the question is deemed poor doesn't mean it shouldn't be answered and definitely isn't a reason to down vote an answer. – juharr Apr 07 '16 at 13:54
  • 1
    @juharr - answering questions like this encourages this sort of behavior. Rather, a comment should be added telling the OP to show what he has tried, to demonstrate some basic knowledge of the topic. As I understood it, SO is not a tutorial site for people to learn **basic** programming concepts but a place where programmers go to help each other with problems. –  Apr 07 '16 at 13:56
  • @PeterA.Schneider I'm not defending the OP or the question, just any SO user's prerogative to answer such a question if they so desire. – juharr Apr 07 '16 at 13:59
  • @juharr - you are still missing the point that answering questions like this encourages this sort of behavior –  Apr 07 '16 at 14:05
  • @Agapwlesu I had just posted the question and 30 seconds after you guys flooded the comments section with this sort of remarks. Probably if you would just give me the time to edit or simply asked me what I did try before asking this question, I would have told you. If you check my edit I have given one of my last attempts in answering the question myself before posting. HAPPY? – Cris Apr 07 '16 at 14:17
  • 3
    @AgapwIesu - Do not badger people providing helpful answers to questions you do not like. Attacking subject matter experts for trying to be helpful will only drive them away, and will not impact in the least the number of people asking lower-quality questions: http://meta.stackoverflow.com/a/255861/19679 – Brad Larson Apr 07 '16 at 14:35
  • @BradLarson I understand your argument and don't want to draw out an OT discussion here; but I would hope that we have fewer low-qual questions if we don't promptly answer them (it's easily to see how a whole CS101 ends up asking for homework here if one student succeeds -- or not, if not). I agree that we shouldn't down-vote correct answers though, but chiding is ok imo. – Peter - Reinstate Monica Apr 07 '16 at 14:59
  • @BradLarson, I disagree with your post on meta, but as this points out, this has morphed into more of a meta question. Will go to your answer there to comment on what I believe you are missing. –  Apr 07 '16 at 15:20