AnotherFineMess
AnotherFineMess

Reputation: 123

C# regex for specific character or nothing

Short version: How do I match a single, specific character or nothing within a longer, potentially repeating, pattern?

Long version: I'm forming a regex to count the occurrences of string 'word' in strings which have the specific format of; a hyphen followed by an integer number (any length) followed by a hyphen followed by the string 'word' followed by a hyphen, potentially repeating. E.g.

'-0-word-' (1 match)
'-10-word-' (1 match)
'-999-word-' (1 match)
'-1-word-1-word-' (2 matches)
'-1-word-1-word-222-word-' (3 matches) etc.

If the pattern repeats then I think the leading hyphen has to be optional as it is already the trailing hyphen for the previous match.

The best I have come up with so far is;

[-]?\d+-word-

which gives 3 matches for

'-1-word-1-word-222-word-'

but it also gives 3 matches for

'-1-word-1-word-X222-word-'

because the leading hyphen is optional and the 'X' is ignored. I want the leading hyphen to be only a hyphen or nothing. I want to make sure the whole string is rejected (no matches) if the format is not correct.

Thanks for your help!

Upvotes: 3

Views: 1099

Answers (3)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

If you want to count the number of occurences and to check the string format at the same time, you can do this:

String input = "-1-word-1-word-222-word-";
String pattern = @"\A(-[0-9]+-word)+-\z";

Match m = Regex.Match(input, pattern);

if (m.Success) {
    Console.WriteLine(m.Groups[1].Captures.Count);
}

When you repeat a capture group, each captures are stored, and you can access them with the Captures attribute.

Upvotes: 1

Karan
Karan

Reputation: 3328

Please try this regex demo for the word occurences.

Pattern is "-\d+-word(-\d+-word)*-"

http://regexr.com/3agkb

Upvotes: 0

vks
vks

Reputation: 67968

^-\d+-word(?:-\d+-word)*-$

Try this.See demo.

https://regex101.com/r/wU7sQ0/20

Upvotes: 4

Related Questions