Omegand
Omegand

Reputation: 47

Remove everything that doesn't match

string line = "Rok rok irrelevant text irrelevant;text.irrelevant,text"; 
string NewLine = Regex.Replace(line, @"\b[rR]\w*", "");

Right now it replaces every word starting with r/R with a blank space, but I want to make everything a blank space EXCEPT words starting with r/R.

Upvotes: 2

Views: 145

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626747

Edit

It seems all you want is to extract words starting with r or R and join them with a space. In this case, use a mere \b[rR]\w* regex and the following code:

var result = string.Join(" ", Regex.Matches(line, @"\b[rR]\w*").Cast<Match>().Select(x => x.Value));

See the C# demo.

Original answer

You may use a negative lookahead after a word boundary:

\b(?![rR])\w+
  ^^^^^^^^

Note that the + quantifier is better here since you want to remove at least 1 char found.

Or, in case you also want to remove all non-word chars after the found word, use

\b(?![rR])\w+\W*

See the regex demo #1 and regex demo #2.

If you want to remove any non-word chars before and after a qualifying word, use

var result = Regex.Replace(line, @"\W*\b(?![rR])\w+\W*", " ").Trim();

It will remove all non-word chars before a word not starting with r and R and after it.

Details

  • \b - a word boundary
  • (?![rR]) - a negative lookahead that will fail the match if, immediately to the right of the current location, there is r or R
  • \w+ - 1+ word chars
  • \W* - 0+ non-word chars.

Upvotes: 3

Related Questions