Question Further Regex assistance sought

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
So I been reading up on Regex like a mad man the last couple of days, but there is something that just isn't clicking.

I have a Regex that does 'almost' exactly what I want:

C#:
names = Regex.Replace(names, @"(^\w)|(\s\w)|(\W\w)", m => m.Value.ToUpper());

This goes through the string and capitalises all the words. Words at the start, words preceded by a space and words that have " in front of them.

All great, except, I would like it if it did not capitalise 'and' / 'or' (would like to add more).

Currently, I am using this:

C#:
names = Regex.Replace(names, @"(\b(And|Or|Any)\b)", m => m.Value.ToLower());

After the above code, which reduces those words back to lower case.

This seems inefficient.

I have been reading about looking ahead to exclude certain words, and I think this code is correct:

C#:
\b(?!and\b)

For a look ahead on "and" - and I tried 'And' just in case it was upper casing the word before the look ahead got to it, I didn't think that was right and didn't make any difference.

My problem is how do I put this in my existing Regex? I have tried various different incarnations, and used a Regex website to look through what was happening (Regex101.com).

It appears to say that the Regex will exclude 'and' literally when it appears as a solo word, but it isn't and it is having a negative effect on my other capitalisation.

C#:
\b(?!and\b)(^\w)|\b(?!and\b)(\s\w)|\b(?!and\b)(\W\w)

That is the basic first attempt, but I have tried it just at the start, with brackets, without brackets, I have tried limiting it down to just one Regex and the look ahead excluding it with and without brackets.

I just can't seem to get it to work. It either just stops working all together, or capitalises the 'and'.

This what I get from Regex101:

First Alternative
\b(?!and\b)(^\w)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
Negative Lookahead
(?!and\b)
Assert that the Regex below does not match
and matches the characters and literally (case sensitive)
\b assert position at a word boundary: (^\w|\w$|\W\w|\w\W)
1st Capturing Group
(^\w)
^ asserts position at start of a line
\w matches any word character (equivalent to [a-zA-Z0-9_])

Even that though, does not work, it does not pick up on the words at the start of a line if I include the exclusion aspect.
 

Latest posts

Back
Top Bottom