Imdad
Imdad

Reputation: 769

Regex in Visual Studio find and replace not working

Trying to use Find and Replace in Visual Studio 2017.

It's seemingly working perfectly on a Regex checking tool that is online but not working in Visual Studio when I do a Find search (Ctrl + F).

Any ideas why this may be? Do I need to do anything differently in VS compared to the standard Regex?

Here is the Regex:

([.material\-icons]+[a-z|A-Z|\-|\_]+:+before,)

Here is a sample set of code/text:

.material-icons.three-d-rotation:before,
.icon-mi-three-d-rotation:before {
    content: '\e84d';
}

.material-icons.ac-unit:before,
.icon-mi-ac-unit:before {
    content: '\eb3b';
}

It is supposed to find

.material-icons.three-d-rotation:before,

and

.material-icons.ac-unit:before,

Upvotes: 3

Views: 2677

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626802

You must remove the backslash before _, \_ makes the .NET regex syntax invalid.

Also, if you need to match a sequence of chars and not chars in any other order and any amount, you should use a mere sequence of those chars without putting them into a character class. Change [.material\-icons]+ into \.material-icons.

Note that - outside of a character class (outside of [...]) does not need escaping.

Inside a character class, | matches a pipe char, it is not an OR operator. Thus, you should remove it from the [...].

There is no need wrapping the whole pattern with parentheses, if you need to replace with the whole match, use the $& placeholder.

You may use

\.material-icons\.[\w.-]+:+before,

enter image description here

Details

  • \.material-icons\. - a literal .material-icons. text
  • [\w.-]+ - 1 or more letters, digits, _, . or -
  • :+ - 1+ colons
  • before, - a literal substring.

Upvotes: 2

Related Questions