2

I want to find (in VS C#) if a string contains a character (for example '%') with no immediate repetitions.

For example "i have % alone and this is fine=>%%" . I want to find any string that contains a single '%' (even a couple times) regardless of adjoined "%%" occurrences.

The following will obviously not work, and will give true for foo2:

string foo1="% I want to find this string";
string foo2="I don't want to find this string because the char %% is not alone";
string foo3="I%want%to%find%this%as%well!"
if(line.Contains("%")){}

I have tried to understand how to apply regular expression here tono avail.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
havakok
  • 1,185
  • 2
  • 13
  • 45
  • 4
    Try [`(?<!%)%(?!%)`](http://regexstorm.net/tester?p=(%3f%3c!%25)%25(%3f!%25)&i=%25+I+want+to+find+this+string%0d%0aI+don%27t+want+to+find+this+string+because+the+char+%25%25+is+not+alone%0d%0aI%25want%25to%25find%25this%25as%25well!) – Wiktor Stribiżew Sep 27 '16 at 11:43
  • 2
    To extend Wiktor's comment, read this: http://stackoverflow.com/questions/2973436/regex-lookahead-lookbehind-and-atomic-groups – kiziu Sep 27 '16 at 11:44
  • The Regex Pattern would be : string pattern = "%[^%]+"; – jdweng Sep 27 '16 at 11:45
  • 2
    Why not just check `if(!line.Contains("%%")){ // string ok }` ? There is no real need for Regex or is there? – Bojan B Sep 27 '16 at 11:46
  • Bojan, great solution! – Dusan Sep 27 '16 at 11:48
  • 1
    Bojan has a point. You can check if data contains any `%`, but does not contain `%%` - should be faster than regex. – kiziu Sep 27 '16 at 11:48
  • What is the expected result for `%hello%%world`. Do you have to consider it? – Sebastian Proske Sep 27 '16 at 12:47

2 Answers2

7

Moving my comment here:

You might just as well use a non-regex approach for that:

if (s.Contains("%") && !s.Contains("%%"))

If you need to use a regex, you may use negative lookarounds with Regex.IsMatch:

if(Regex.IsMatch(line, @"(?<!%)%(?!%)")) {}

See this regex demo.

The (?<!%) negative lookbehind will fail the match if a % is preceded with a % and the (?!%) negative lookahead will fail the match if the % is followed with %.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 2
    Well, I guess it can also be done like `if (s.Contains("%") && !s.Contains("%%"))`. Choose to your liking :) – Wiktor Stribiżew Sep 27 '16 at 11:48
  • I did the decent thing and deleted my answer when I realised others had beat me to it with comments and another answer. Then I downvoted yours as the regex seemed overkill. Now you have put the simple solution in your answer, I've reversed that to an upvote. :) – David Arno Sep 27 '16 at 11:58
  • @DavidArno: I am very glad you explain that but a regex is not overkill. In some cases, when there is no access to C# code itself, only a regex can help. Although for OP that does not make any difference, other users that are seeking a .NET regex will find a code only answer insufficient. More, sometimes a regex is better than pure code (when there are specific requirements and again it is not possible to change code much). Both solutions have equal right to be called "good" and "working" and the regex solution does not deserve downvoting. BTW, the regex is quite efficient, test it,you'll see – Wiktor Stribiżew Sep 27 '16 at 12:03
0

I'll post as an answer since comments could be overlooked by other users sometimes.

So a simple solution if regex is not necessary could be:

if(line.Contains("%") && !line.Contains("%%"))
{ 
  // string ok 
}

As pointed out by others, you have to check if the % is contained in the string at all.

Bojan B
  • 2,091
  • 4
  • 18
  • 26