
¿Alguien puede explicar cómo al final de cada línea coloco una tubería con expresiones regulares?
Usando buscar y reemplazar en el bloc de notas ++
Estoy tratando de colocarcontent |
¡Salud!
Respuesta1
Si solo desea agregar una tubería al final de cada línea, simplemente use esta expresión regular 'buscar':
$
(El símbolo del dólar coincide con el final de una línea en expresiones regulares)
Y este 'reemplazar':
|
Asegúrese de haber habilitado la búsqueda de expresiones regulares.
Respuesta2
Encontrar que:(.*)
Reemplazar con:$1|
Esto usaagrupamiento, $1
básicamente significa insertar lo que se encontró entre paréntesis y luego agregar la tubería al final. Capturará .
cualquier carácter excepto ciertos espacios en blanco, como nuevas líneas, lo cual es ideal en esta situación. Los *
medios permiten capturar 0 o más caracteres con el.
Esto es escalable, por lo que si desea capturar solo cierta línea, por ejemplo, líneas que contienen prueba:
Encontrar que:(.*test.*)
Reemplazar con:$1|
Entonces, si los ingresa y luego presiona "Reemplazar todo", tendrá tuberías al final de cada línea que coincidan con la expresión regular.
Respuesta3
Respuesta cambiada.
Yo usaría esto. Trabajo en modo de una o varias líneas.
No estoy seguro de qué bloc de notas tiene disponible (es decir, buscar/reemplazar, buscar/reemplazar, etc.).
Busque esto: (?=\r\n|\n|\r|$)
e inserte (reemplace) esto: |
Sólo algunas notas sobre ese complicado tema de lo que realmente es $metachar.
No es tan fácil como debería ser y los Docs dejan mucho que desear.
De todos modos aquí está mi opinión al respecto:
# Regular Expression Docs:
# Metacharacter $
# Match the end of the line (or before newline at the end)
# ** This really is misworded, it really means:
#
# In single line mode,
# In two separate matches (in global context)
# Match before and after a newline at the end of a string if there is a newline,
# OR, Match the end of the string only.
#
# In multi-line mode,
# Match before OR after a newline at the end of a line or string,
# but not both.
#
# ---------------------------------------------
# The above explanation is conditional upon the qualifying
# subexpressions surrounding the $ metachar
# ---------------------------------------------
# /$/g Single line mode:
# Matches before newline (if there is one) AND end of string (always this)
print "=== /\$/g ===============\n";
$str = "0 "; $str =~ s/$/|/g; print "'$str'\n---\n";
$str = "1 \n"; $str =~ s/$/|/g; print "'$str'\n---\n";
$str = "2 \n\n"; $str =~ s/$/|/g; print "'$str'\n---\n";
$str = "3 \n\n\n"; $str =~ s/$/|/g; print "'$str'\n---\n";
$str = "4 \n\n\n\n"; $str =~ s/$/|/g; print "'$str'\n\n";
# /$/mg Multi-line mode:
# Matches before each newline (if there is one) OR end of string (not both)
print "=== /\$/mg ===============\n";
$str = "0 "; $str =~ s/$/|/mg; print "'$str'\n---\n";
$str = "1 \n"; $str =~ s/$/|/mg; print "'$str'\n---\n";
$str = "2 \n\n"; $str =~ s/$/|/mg; print "'$str'\n---\n";
$str = "3 \n\n\n"; $str =~ s/$/|/mg; print "'$str'\n---\n";
$str = "4 \n\n\n\n"; $str =~ s/$/|/mg; print "'$str'\n\n";
# /(?=\r\n|\n|\r|$)/g Single line mode:
# Parsing the expression for //m Multi-line mode,
# Equivalent of /$/m can now be run in Single line mode:
# This is What /$/m probably really is.
# Matches before each newline (if there is one) OR end of string (not both)
print "=== /(?=\\r\\n|\\n|\\r|\$)/g ==============\n";
$str = "0 "; $str =~ s/(?=\r\n|\n|\r|$)/|/g; print "'$str'\n---\n";
$str = "1 \n"; $str =~ s/(?=\r\n|\n|\r|$)/|/g; print "'$str'\n---\n";
$str = "2 \n\n"; $str =~ s/(?=\r\n|\n|\r|$)/|/g; print "'$str'\n---\n";
$str = "3 \n\n\n"; $str =~ s/(?=\r\n|\n|\r|$)/|/g; print "'$str'\n---\n";
$str = "4 \n\n\n\n"; $str =~ s/(?=\r\n|\n|\r|$)/|/g; print "'$str'\n\n";
# /(?=\r\n|\n|\r|$)/mg Multi-line mode:
# Exact same output.
print "=== /(?=\\r\\n|\\n|\\r|\$)/mg ==============\n";
$str = "0 "; $str =~ s/(?=\r\n|\n|\r|$)/|/mg; print "'$str'\n---\n";
$str = "1 \n"; $str =~ s/(?=\r\n|\n|\r|$)/|/mg; print "'$str'\n---\n";
$str = "2 \n\n"; $str =~ s/(?=\r\n|\n|\r|$)/|/mg; print "'$str'\n---\n";
$str = "3 \n\n\n"; $str =~ s/(?=\r\n|\n|\r|$)/|/mg; print "'$str'\n---\n";
$str = "4 \n\n\n\n"; $str =~ s/(?=\r\n|\n|\r|$)/|/mg; print "'$str'\n\n";
Salida >>
=== /$/g ===============
'0 |'
---
'1 |
|'
---
'2
|
|'
---
'3
|
|'
---
'4
|
|'
=== /$/mg ===============
'0 |'
---
'1 |
|'
---
'2 |
|
|'
---
'3 |
|
|
|'
---
'4 |
|
|
|
|'
=== /(?=\r\n|\n|\r|$)/g ==============
'0 |'
---
'1 |
|'
---
'2 |
|
|'
---
'3 |
|
|
|'
---
'4 |
|
|
|
|'
=== /(?=\r\n|\n|\r|$)/mg ==============
'0 |'
---
'1 |
|'
---
'2 |
|
|'
---
'3 |
|
|
|'
---
'4 |
|
|
|
|'