-2

I have the following regex string "(\"\--[a-zA-Z]+\")" but I can not use it since it uses a double quotation. What are the escape characters I could try here?

I want to use as follows in C# code to filter the "KernalSize" from the following string :

parser.add_argument("--KernalSize", default = 6, help = "Noise removal")

Following filter (\"\--[a-zA-Z]+\") returns "--KernalSize". However, when I try to set the filter in C# code, it failed due to a double quotation sign within the string.

const string ParamNameFilter = @"(\"\--[a-zA-Z]+\")";
PCG
  • 2,049
  • 5
  • 24
  • 42
  • Like this? `string pattern = @"""--([a-zA-Z]+)""";` – The fourth bird Oct 08 '20 at 19:19
  • There is generally no problem to have `"` in C# string literal or string in general. Could you please [edit] question to clarify what you mean "can not use it since it uses a double quotation"? – Alexei Levenkov Oct 08 '20 at 19:20
  • @Thefourthbird that is no different from what OP has already... (as "" and \" are resulting in the same value...) can you please clarify why you think it is better? – Alexei Levenkov Oct 08 '20 at 19:21
  • @AlexeiLevenkov I did not say I think that it is better, it was a question if that would work. – The fourth bird Oct 08 '20 at 19:22
  • 1
    @Thefourthbird sorry, you are right. OP simply need to read (censored, suggesting to read documentation is unacceptable) about string literals and difference between`@".."` and `"..."`. – Alexei Levenkov Oct 08 '20 at 19:27
  • @AlexeiLevenkov No worries, all is well :-) – The fourth bird Oct 08 '20 at 19:30
  • 1
    Also, an exact dupe of https://stackoverflow.com/questions/3905946/how-to-add-double-quotes-to-a-string-that-is-inside-a-variable – Wiktor Stribiżew Oct 08 '20 at 20:24
  • What was the _typo or not reproducible_ items in the post ? Was it really closed for that ? –  Oct 08 '20 at 20:34
  • @Maxt8r there are plenty ways to mistype string :) also it is mostly "While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers." Indeed it could be closed as duplicate instead as Wictor found... but generally questions are not re-opened just to be closed with different reason. Feel free to bring this to meta if you need more discussion. – Alexei Levenkov Oct 08 '20 at 21:26

2 Answers2

1

so your regex string (\"\--[a-zA-Z]+\") should match strings like "--AnythingAlpha"?

in reality the backslash before one of the - is not even necessary, so it begs a question if there is any type of a typo there, as it is equivalent to just (\"--[a-zA-Z]+\") or even a simpler (\"--[a-z]+\") with case-insensitive flag.

Now it's just about representing this string in C# strings: (\"\--[a-zA-Z]+\") => to one of

  • @"(\""\--[a-zA-Z]+\"")"
  • "(\\\"\\--[a-zA-Z]+\\\")"

you can test them with something like:

  public static void Main (string[] args) 
  {
    Console.WriteLine(@"(\""\--[a-zA-Z]+\"")");
    Console.WriteLine("(\\\"\\--[a-zA-Z]+\\\")");
  }
aiodintsov
  • 2,545
  • 15
  • 17
0

The two quoting options are

Double quote "(\"--[a-zA-Z]+\")"

Verbatim @"(""--[a-zA-Z]+"")"

Note inside a verbatim string, the double quote is escaped by double quoting it.