开发者

Need regular expression to remove one and only one s

Need a regex to remove letter s

I want regex to remove s only if it is the last characte and only o开发者_C百科ne instance is found

So word "boys" will become "boy"

and word "process" will remain the same since there are more than one "s" and should be case insensitive.

Please help Thanks


Works only on a single word. I tried this code below and does not work in a sentence.

string str = "The boys in the student's class didn't understand  the process.";
str = System.Text.RegularExpressions.Regex.Replace(str, "([^s])s$", "$1");
Console.WriteLine(str);
Console.ReadLine();


I believe what you need is a zero-width negative look-behind assertion; the Perl-style regular expression would be something like this:

$line =~ s/(?<!s)s\b//gi;

Which means "remove a single 's' character if it is the last character of a word (i.e. a whitespace character is the next character) and if the preceding character is not an 's', all case insensitive."

use strict;
use warnings;

my $line = "The boys in the student's class didn't understand the process.";

$line =~ s/(?<!s)s\b//gi;

print "$line\n";

yields: "The boy in the student' class didn't understand the process."


This should work:

/([^s])s$/\1/

This in say C# is:

Regex.Replace(str, "([^s])s$", "$1")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜