0

I have a hard time solving this problem I need to get all the word that contains apostrophe, sometimes the single used as apostrophe

This is my regex:

[\u0027\u2019]

And this is my sample words:

'Chapert 2' 
Chapter's
chapters'

My expected output is

Chapter's
chapters'

But my code right now is it gets all the word that contains apostrophe or single qoute. How can i get all the word that doesn't start with apostrophe or single qoute?

Ramon bihon
  • 385
  • 1
  • 5
  • 18

2 Answers2

2

This should give you the desired result:

Regex rgx = new Regex(@"^[^'].*'.*$");
List<string> list = new List<string>() { "'Chapert 2'", "Chapter's", "chapters'" };

foreach(var item in list)
{
    if (rgx.IsMatch(item))
        Console.WriteLine(item);
}

Breakdown

  • ^ - Start of string.
  • [^'] - One character that is NOT the '.
  • .* - Zero or more any characters.
  • ' - There has to be at leas one '
  • .* - Zero or more any characters again to end the string.
  • $ - End of string.
Sach
  • 10,091
  • 8
  • 47
  • 84
1

^[^'] is enough.

  • The first ^ matches the beginning of a string
  • [^…] matches every character except the ones inside brackets

So ^[^'] matches strings that start with a non-apostrophe character.

  • This won't work. Not only they shouldn't start with an apostrophe, OP also says that, _I need to get all the word that contains apostrophe_. – Sach Aug 02 '18 at 16:20
  • Alright, I'm embarrassed for my carelessness. I forgot what you said at the beginning. – Xiaofeng Zheng Aug 03 '18 at 04:29