Eu tenho string como
(1)ABC(Some other text)
(2343)DEFGHIJ
(99)Q
Eu queria um regex que capturasse essas cordas em dois grupos, assim
ist: (1) 2nd: ABC(Some other text)
1st: (2343) 2nd: DEFGHIJ
ist: (99) 2nd: Q
Então eu escrevi este Regex
var regex new Regex("^\((\d+)(.*)\)");
Match match = regex.Match(str);
Mas em vez dos dois grupos, eu esperava três grupos
No primeiro exemplo eu recebo
(1)ABC(Some other text)
1
)ABC(Some other text
O que está errado?
Respostas:
2 para resposta № 1O regex que você está procurando é provavelmente
@"^((d+))(.*)"
Você reverteu a ordem do (
. Note que os grupos serão 3, porque, como alguém apontou, o grupo 0 é todo o texto correspondente. assim
string str = "(1)ABC(Some other text)";
var regex = new Regex(@"^((d+))(.*)");
Match match = regex.Match(str);
if (match.Success)
{
string gr1 = match.Groups[1].Value; // (1)
string gr2 = match.Groups[2].Value; // (Some other text)
}