2010-08-29 19 views
6

Esta es una expresión regular bien documentada, fácil de entender, mantener y modificar.¿Cómo entiendes las expresiones regulares que están escritas en una línea?

text = text.replace(/ 
    (        // Wrap whole match in $1 
     (
      ^[ \t]*>[ \t]?   // '>' at the start of a line 
      .+\n     // rest of the first line 
      (.+\n)*     // subsequent consecutive lines 
      \n*      // blanks 
     )+ 
    ) 
    /gm, 

Pero, ¿cómo trabajas para trabajar con estos?

text = text.replace(/((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)/gm, 

¿Existe un beautifier de algún tipo que da sentido y describe su funcionalidad?

+1

Tal vez algunas herramientas en http://stackoverflow.com/questions/32282/regex-testing-tools ayudará. – kennytm

+0

Yo tampoco. Siempre uso 6-10 líneas de código con explode/join/strstr/substr (PHP) en su lugar. Más fácil de entender, mantener e incluso escribir. – ern0

+0

No todos los idiomas o bibliotecas que admiten expresiones regulares funcionarán tan limpiamente como su ejemplo, debido a los espacios en blanco adicionales. –

Respuesta

3

RegexBuddy "traducirá" cualquier expresión regular para usted. Cuando se alimenta el ejemplo de expresiones regulares, que da salida:

((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+) 

Options:^and $ match at line breaks 

Match the regular expression below and capture its match into backreference number 1 «((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)» 
    Match the regular expression below and capture its match into backreference number 2 «(^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+» 
     Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
     Note: You repeated the capturing group itself. The group will capture only the last iteration. 
      Put a capturing group around the repeated group to capture all iterations. «+» 
     Assert position at the beginning of a line (at beginning of the string or after a line break character) «^» 
     Match a single character present in the list below «[ \t]*» 
     Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
     The character “ ” « » 
     A tab character «\t» 
     Match the character “>” literally «>» 
     Match a single character present in the list below «[ \t]?» 
     Between zero and one times, as many times as possible, giving back as needed (greedy) «?» 
     The character “ ” « » 
     A tab character «\t» 
     Match any single character that is not a line break character «.+» 
     Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
     Match a line feed character «\n» 
     Match the regular expression below and capture its match into backreference number 3 «(.+\n)*» 
     Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 
     Note: You repeated the capturing group itself. The group will capture only the last iteration. 
      Put a capturing group around the repeated group to capture all iterations. «*» 
     Match any single character that is not a line break character «.+» 
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» 
     Match a line feed character «\n» 
     Match a line feed character «\n*» 
     Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*» 

Esto se parece bastante intimidante en forma de texto, pero es mucho más fácil de leer en el formulario HTML (que no puede ser reproducido aquí) o en la misma RegexBuddy. También señala problemas comunes (como la repetición de grupos de captura que probablemente no se desee aquí).

+0

Guau, es bastante detallado. Pero podría ser útil para aprender Regex. –

+0

Hablando francamente, encuentro que estas pesadas explicaciones no son mucho más comprensibles que las expresiones regulares de 1 línea. – kennytm

0

Después de un tiempo, me he acostumbrado a leer las cosas. No hay mucho para la mayoría de los regex, y yo recomiendo el sitio http://www.regular-expressions.info/ si quiere usarlos con más frecuencia.

5

Merece la pena esforzarse para leer expresiones regulares en el formato de una línea. La mayoría de las veces están escritas de esta manera

+0

Sino como cualquier sintaxis del lenguaje de programación, después de un tiempo se vuelve legible. – Chris

0

Las expresiones regulares son solo una forma de expresar máscaras, etc. Al final es solo un "lenguaje" con su propia sintaxis.
Comentar cada bit de su expresión regular sería lo mismo que comentar cada línea de su proyecto.
Por supuesto que ayudaría a las personas que no entienden su código, pero es inútil si usted (el desarrollador) entiende el significado de la expresión regular.

Para mí, leer expresiones regulares es lo mismo que leer el código. Si la expresión es realmente compleja, una explicación a continuación podría ser útil, pero la mayoría de las veces no es necesaria.

Cuestiones relacionadas