c# - Unable to use verbatim string in Regex constructor instantiation -


i'm attempting new-up regex object using pattern. string pattern , original regex below:

string search = "root\\hill"; var regex = new regex(search, regexoptions.ignorecase); 

this throws system.argumentexception exception i'd convert pattern verbatim string. i've tried this:

var regex = new regex(@search, regexoptions.ignorecase); 

and this:

string verbatim = @search; var regex = new regex(verbatim , regexoptions.ignorecase); 

but no avail. both throw same exception. when i'm debugging, putting "raw" string in regex constructor (eg. new regex(@"root\\hill", regexoptions.ignorecase)) works, search value changes of course.

how can use verbatim string variable?

the @ sign must before string literal, not before variable name:

string search = @"root\\hill"; var regex = new regex(search, regexoptions.ignorecase); 

putting @ sign before identifier way use language keyword identifier, it's unrelated verbatim strings.


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -