RegEx Matching Strings

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 pruebabut should not match CONFIGTORNEO_ID, because they are written between square brackets ( [])

In short, I want to match a string between double quote characters ( "), but this string cannot 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]);
}

This pattern matches the entire line between characters ", but how can I change the pattern to exclude those lines that are written between square brackets?

- change ---

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++;
    }

MESA_ID ( Cells["MESA_ID"].Value) Puesto ( Cells["Puesto"].Value). ].Value == DBNull.Value && dr.Cells[ ( ["MESA_ID"].Value == DBNull.Value && dr.Cells["Puesto"])

, .

+3
4

:

(?<!\[)

, , [. :

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]);
}
+2

, , :

  • , , [
  • ]

:

(?<!\[\s*)\"[^"]*\"(?!\s*\])

lookaround .NET regexp engine.

, ? , [^"]* .*?.

+1

, - :

^[^\"]*\"([^\"]*)\".*$
0

(php | cpp | java | js | css | ..) . /, , .

: /(['"])(\\\1|.)*?\1/gm :

  • , single | double quote: ['"]
  • , ( ), - ( escape \): (\\\1|.)*
  • , , (.. ): ?
  • , , | double quote: \1

, ( ), ( , CRLF, ?)

You might be interested in not only finding, but also capturing these groups of strings, so make sure you put in the group separator (\\\1|.)*?that gives the final pattern:

([\'"])((\\\1|.)*?)\1

Examples of captured lines:

defined ( 'WP_DEBUG' ) || define( '\WP_DEBUG', true );
echo 'class="input-text card-number" type="text" maxlength="20"';
echo 'How are you? I\'m fine, thank you';

Check out my pattern in the online regex test .

0
source

All Articles