I have a question related to regular expressions in c#.
I want to find text between " characters. Example:
Enum resultado = SPDialogBox.Instance.show<ACTION_ENUMs.TORNEO_SORTEAR>("Esto es una prueba");
Matches: Esto es una prueba
But, in this example
Enum resultado = SPDialogBox.Instance.show<ACTION_ENUMs.TORNEO_SORTEAR>("Esto es una prueba");
pKR_MESAPUESTOASIGNACION.CONFIGTORNEO_ID = Valid.GetInt(dr.Cells["CONFIGTORNEO_ID"].Value);
Matches: Esto es una prueba but must not match CONFIGTORNEO_ID, because it is written between square brackets ([])
In brief, I want to match string between double quote (") characters, but that string must not be written between square brackets ([]).
Here is my code:
var pattern = "\"(.*?)\"";
var matches = Regex.Matches(fullCode, pattern, RegexOptions.Multiline);
foreach (Match m in matches)
{
Console.WriteLine(m.Groups[1]);
}
That pattern matches all string between " characters, but how can I modify the pattern to exclude those string that are written between square brackets?
-- edit ---
here is another example:
List<String> IdSorteados = new List<String>();
int TablesToSort = 0;
foreach (UltraGridRow dr in fg.hfg_Rows)
{
if (dr.Cells["MESA_ID"].Value == DBNull.Value && dr.Cells["Puesto"].Value == DBNull.Value && !Valid.GetBoolean(dr.Cells["BELIMINADO"].Value) && (Valid.GetBoolean(dr.Cells["Seleccionado"].Value) || SortearTodo))
TablesToSort++;
}
The expression must not match MESA_ID ( found within Cells["MESA_ID"].Value ) nor Puesto (found within Cells["Puesto"].Value ). It also must not match ].Value == DBNull.Value && dr.Cells[ (found within ["MESA_ID"].Value == DBNull.Value && dr.Cells["Puesto"] )
I hope I have made my intent clear.
Simple use a negative look-behind:
(?<!\[)
Basically, only match a string when not preceded by a [. Example here, and code as follows:
String fullCode = "Enum resultado = SPDialogBox.Instance.show<ACTION_ENUMs.TORNEO_SORTEAR>(\"Esto es una prueba\");\r\n"
+ "pKR_MESAPUESTOASIGNACION.CONFIGTORNEO_ID = Valid.GetInt(dr.Cells[\"CONFIGTORNEO_ID\"].Value);";
String pattern = @"(?<!\[)\x22(.*?)\x22";
var matches = Regex.Matches(fullCode, pattern, RegexOptions.Multiline);
foreach (Match m in matches)
{
Console.WriteLine(m.Groups[1]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With