Reputation: 4687
This match is giving me no matches, seemingly no matter what:
static String divine(int n)
{
if (n % 3 == 0) { return String.Join("", Enumerable.Repeat("5", n).ToArray()); }
String res = String.Join("", Enumerable.Repeat(" ", n));
Regex ItemRegex = new Regex(@"^(\\s{3}){0,}(\\s{5})*$", RegexOptions.Compiled);
Match match = ItemRegex.Match(res);
But this PCRE regex is behaving perfectly at regex101.com: ^(\s{3}){0,}(\s{5})*$
It always greedily matches the small group (sets of 3), and matches the large group only as needed to have no spaces unmatched at the end.
My question is, what do I have to do to get the regex to behave as expected in c#? If I had to guess, at this point I am leaning towards {,}
maybe being illegal? I don't know.
Upvotes: 0
Views: 45
Reputation: 6173
I can see Java's influence. The @
means you do not need the evil escaped escape \\
:
@"^(\s{3}){0,}(\s{5})*$"
In case you didn't know, Java's regexes are a pain. Most languages aren't like that. Python, for example, uses r
instead of C#'s @
.
Edit: JavaScript only makes you escape escapes within strings, but you can use something like: var word2 = word.replace(/\s/g, "");
. The syntax is similar to Perl, actually.
Java has no alternative to escaped escapes, which is why I assumed you meant Java.
Upvotes: 2