How to execute regex in string with quotes in C #

Given the line:

/home   "1020....2010" main 

I would like to use home, 1020and 2010using regex, but I still have problems with the quote. Can someone help me?


Thank you guys for your post. I understand that my code may have problems. Here

string pattern [1] = @ "blablabla"; string pattern [2] = @ "blablabla"; ......

foreach (line s in the template) {if (regex.match (line, s) .success) {......}}

Then an error occurs indicating the unexpected character "\"

+3
source share
3 answers

Use the template:

/(\w+)\s+"(\d+)\.+(\d+)"

home ( /) $1, $2 $3.

1

, @ , ideone.com. :

using System;
using System.Text.RegularExpressions;

public class RegexTest 
{
    public static void Main() 
    {
        Regex r = new Regex("/(\\w+)\\s+\"(\\d+)\\.+(\\d+)\""); 
        Match m = r.Match("/home   \"1020....2010\" main ");
        Console.WriteLine("$1 = " + m.Groups[1]);
        Console.WriteLine("$2 = " + m.Groups[2]);
        Console.WriteLine("$3 = " + m.Groups[3]);
    }
}

:

$1 = home
$2 = 1020
$3 = 2010

: http://ideone.com/TpQwf

2

@Seattle, @ , , ( !):

Regex r = new Regex(@"/(\w+)\s+""(\d+)\.+(\d+)"""); 
+6

- :

/(\w+) "(\d+)\.+(\d+)" (\w+)

:

0

If you want to match the quote character (or other special characters), you can avoid this by using a backslash. Therefore, \"in your regular expression will match the character.

0
source

Source: https://habr.com/ru/post/1775273/


All Articles