Home > Back-end >  Regex that allows " ' " only in word ex. "won't" would be allowed but
Regex that allows " ' " only in word ex. "won't" would be allowed but

Time:07-18

I have a code here

Regex regex = new Regex(s);
    if (s.Length > 0 && Regex.IsMatch(s, "^\'?"))
    {
        s = Regex.Replace(s, "[^a-zA-Z'] ", " ").ToLower();

My goal is to allow " ' symbol only if it's in a word like "won't" but not allowing if it's " ''' " for example I tried some but it doesn't seem to work.

CodePudding user response:

using System.Text.RegularExpressions;

var s = "There won't be an ' at the end of theirs' but then it's 'bout right.";

Console.WriteLine(s);

var s1 = Regex.Replace(s, "(?<g1>[^a-zA-Z])'(?<g2>[^a-zA-Z])", "${g1} ${g2}").ToLower();
Console.WriteLine(s1);

var s2 = Regex.Replace(s, "(?<g1>[^a-zA-Z])'(?<g2>[^a-zA-Z])", "${g1}${g2}").ToLower();
Console.WriteLine(s2);

var s3 = Regex.Replace(s, "(?<g1>[^a-zA-Z])'(?<g2>[^a-zA-Z])|(?<g1>[^a-zA-Z])'(?<g2>[a-zA-Z])|(?<g1>[a-zA-Z])'(?<g2>[^a-zA-Z])", "${g1} ${g2}").ToLower();
Console.WriteLine(s3);

var s4 = Regex.Replace(s, "(?<g1>[^a-zA-Z])'(?<g2>[^a-zA-Z])|(?<g1>[^a-zA-Z])'(?<g2>[a-zA-Z])|(?<g1>[a-zA-Z])'(?<g2>[^a-zA-Z])", "${g1}${g2}").ToLower();
Console.WriteLine(s4);

The first one allows ' at the start and end of words, and does not allow it as a stand-alone character, replacing it with a space.

The second one allows ' at the start and end of words, and does not allow it as a stand-alone character, deleting it altogether.

The third and fourth do not allow it at the start or end of a word, replacing it with a space or deleting it respectively.

This outputs:

There won't be an ' at the end of theirs' but then it's 'bout right.
there won't be an   at the end of theirs' but then it's 'bout right.
there won't be an  at the end of theirs' but then it's 'bout right.
there won't be an   at the end of theirs  but then it's  bout right.
there won't be an  at the end of theirs but then it's bout right.

CodePudding user response:

I would just loop over the characters and test whether every ' character is adjacent with two letters

otherwise you can use the regex way (there must be a better alternative):

bool allowed = (Regex.IsMatch("don't", "^([^']|([a-zA-Z]'[a-zA-Z]))*$"));//true
bool allowed = (Regex.IsMatch("dont'", "^([^']|([a-zA-Z]'[a-zA-Z]))*$"));//false
  •  Tags:  
  • c#
  • Related