Difference between revisions of "Search and Replace Tricks"

From EditPlus Wiki
Jump to: navigation, search
(by ElfQrin)
 
(Turn a list of names last first(s) into first(s) last, Remove e-mail addresses, Remove note numbers)
 
(54 intermediate revisions by 19 users not shown)
Line 1: Line 1:
==Delete all empty lines==
+
==Suppress all empty lines==
Find: "^\n" (without quotes)
+
Find: "^\n" ''(without quotes)''<br />
Replace: "" (without quotes)
+
Replace: "" ''(without quotes)''<br />
RegExp
+
RegExp<br />
 +
Note: You may have to repeat it several times to remove all empty lines (until the editor can't find the string to replace), because only the first one of a series of consecutive empty lines will be edited.
  
==Truncate last value from a MySQL query==
+
==Suppress all non-empty lines==
Find: (VALUES \('...', '.+'), .+\);
+
Find: "^.+$" ''(without quotes)''<br />
Replace: \1);
+
Replace: "" ''(without quotes)''<br />
RegExp
+
RegExp<br />
  
==Keep only last value in a MySQL query==
+
==Comment out lines==
Find: (VALUES \(.+,)(.+\);)
+
Find: "^" ''(without quotes)''<br />
Replace: VALUES (\2
+
Replace: "// " ''(without quotes)''<br />
RegExp
+
RegExp<br />
 +
Note: Empty lines will not be affected. This example actually adds a text at the beginning of every non empty line, the comment symbol used ("//") is the one of PHP.
 +
 
 +
==Comment lines==
 +
Find: "$" ''(without quotes)''<br />
 +
Replace: " // Comment here" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: Empty lines will not be affected. This example actually adds a text at the end of every non empty line, the comment symbol used ("//") is the one of PHP.
 +
 
 +
==Join lines==
 +
Find: "\n" ''(without quotes)''<br />
 +
Replace: "," ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: Turn separated lines into comma separated values. Empty lines will be affected as well.
 +
 
 +
==Split lines==
 +
Find: "," ''(without quotes)''<br />
 +
Replace: "\n" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: Turn comma separated values into separated lines. Consecutive commas will generate empty lines.
 +
 
 +
==Remove all leading spaces and tabs from every line==
 +
Find: "^[ \t]+" ''(without quotes)''<br />
 +
Replace: "" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: this will obviously remove any indentation<br />
 +
 
 +
==Remove all trailing spaces and tabs from every line==
 +
Find: "[ \t]+$" ''(without quotes)''<br />
 +
Replace: "" ''(without quotes)''<br />
 +
RegExp<br />
 +
 
 +
==Remove consecutive spaces==
 +
Find: "&nbsp;&nbsp;+" ''(without quotes)''<br />
 +
Replace: " " ''(without quotes)''<br />
 +
RegExp<br />
 +
Example: before: "this&nbsp;&nbsp;is an&nbsp;&nbsp;&nbsp;example", after: "this is an example"
 +
 
 +
==Fix punctuation==
 +
Find: "([\.\,\;\:\?\!])([a-zA-Z])" ''(without quotes)''<br />
 +
Replace: "\1 \2" ''(without quotes)''<br />
 +
RegExp<br />
 +
Example: before: "How are you?I'm fine,thanks.", after: "How are you? I'm fine, thanks."<br />
 +
Note: If you want to use this statement in a Wiki context, remove "\:" from the search string, otherwise you may break Wiki metatags, for example <nowiki>[[Category:Example]]</nowiki> will be replaced by <nowiki>[[Category: Example]]</nowiki>
 +
 
 +
==Wrap a tag pair around a text==
 +
Find: "(bold)" ''(without quotes)''<br />
 +
Replace: "<nowiki><b>\1</b></nowiki>" ''(without quotes)''<br />
 +
RegExp<br />
 +
Example: before: "bold", after: "<nowiki><b>bold</b></nowiki>"
 +
 
 +
==Wrap a tag pair around every line==
 +
Find: "^(.+)$" ''(without quotes)''<br />
 +
Replace: "<nowiki><li>\1</li></nowiki>" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: this example wraps the tag pair <nowiki><li></li></nowiki> every line to create a list of items; you'd probably want to apply changes only to a selected text.
 +
 
 +
==Delete everything inside a tag pair (keeping tags)==
 +
Find: "(<delete>).+(</delete>)" ''(without quotes)''<br />
 +
Replace: "\1\2" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: Tags must be on the same line.<br />
 +
Example: before: "this <delete>is an</delete> example", after: "this <delete></delete> example"
 +
 
 +
==Delete everything inside a tag pair (removing tags too)==
 +
Find: "<delete>.+</delete>" ''(without quotes)''<br />
 +
Replace: "" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: Tags must be on the same line.<br />
 +
Example: before: "this <delete>is an</delete> example", after: "this&nbsp;&nbsp;example"
 +
 
 +
==Delete all lines containing a given string==
 +
Find: "^.*STRING.*$" ''(without quotes)''<br />
 +
Replace: "" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: Lines will be emptied but not suppressed. See [[#Suppress all empty lines]] to suppress empty lines.
 +
 
 +
==Replace only the first occurrence of a character==
 +
Find: ";(.*)" ''(without quotes)''<br />
 +
Replace: "|\1" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: In this example, only the first occurrence of ";" for each line will be replaced with "|".<br />
 +
Example: before: "this;is;an;example", after: "this|is;an;example"
 +
 
 +
==Replace only the last occurrence of a character==
 +
Find: "(.*);" ''(without quotes)''<br />
 +
Replace: "\1|" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: In this example, only the last occurrence of ";" for each line will be replaced with "|".<br />
 +
Example: before: "this;is;an;example", after: "this;is;an|example"
 +
 
 +
==Truncate a string at the first occurrence of a marker (keeping the marker)==
 +
Find: "^([^;]*;).*$" ''(without quotes)''<br />
 +
Replace: "\1" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: In this example, the marker is ";".<br />
 +
Example: before: "this;is;an;example", after: "this;"
 +
 
 +
==Truncate a string at the first occurrence of a marker (removing the marker too)==
 +
Find: "^([^;]*);+.*$" ''(without quotes)''<br />
 +
Replace: "\1" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: In this example, the marker is ";".<br />
 +
Example: before: "this;is;an;example", after: "this"
 +
 
 +
==Truncate a string at the last occurrence of a marker (keeping the marker)==
 +
Find: "^(.*;).*$" ''(without quotes)''<br />
 +
Replace: "\1" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: In this example, the marker is ";".<br />
 +
Example: before: "this;is;an;example", after: "this;is;an;"
 +
 
 +
==Truncate a string at the last occurrence of a marker (removing the marker too)==
 +
Find: "^(.*);.*$" ''(without quotes)''<br />
 +
Replace: "\1" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: In this example, the marker is ";".<br />
 +
Example: before: "this;is;an;example", after: "this;is;an"
 +
 
 +
==Truncate a string up to the first occurrence of a marker (keeping the marker)==
 +
Find: "^[^;]*(;.*)$" ''(without quotes)''<br />
 +
Replace: "\1" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: In this example, the marker is ";".<br />
 +
Example: before: "this;is;an;example", after: ";is;an;example"
 +
 
 +
==Truncate a string up to the first occurrence of a marker (removing the marker too)==
 +
Find: "^[^;]*;(.*)$" ''(without quotes)''<br />
 +
Replace: "\1" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: In this example, the marker is ";".<br />
 +
Example: before: "this;is;an;example", after: "is;an;example"
 +
 
 +
==Truncate a string up to the last occurrence of a marker (keeping the marker)==
 +
Find: "^.*(;.*)$" ''(without quotes)''<br />
 +
Replace: "\1" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: In this example, the marker is ";".<br />
 +
Example: before: "this;is;an;example", after: ";example"
 +
 
 +
==Truncate a string up to the last occurrence of a marker (removing the marker too)==
 +
Find: "^.*;(.*)$" ''(without quotes)''<br />
 +
Replace: "\1" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: In this example, the marker is ";".<br />
 +
Example: before: "this;is;an;example", after: "example"
 +
 
 +
==Turn a list of names last first(s) into first(s) last==
 +
Find "^([^ ]*) (.*)$" ''(without quotes)''<br />
 +
Replace: "\2 \1" ''(without quotes)''<br />
 +
RegExp<br />
 +
Note: Names are separated by a space character " ".<br />
 +
Example: before: "Smith John Jack", after: "John Jack Smith"
 +
 
 +
==Remove e-mail addresses==
 +
Find "[a-zA-Z0-9\.\-_]+@[a-zA-Z0-9\.\-_]+\.[a-zA-Z0-9\.\-_]+" ''(without quotes)''<br />
 +
Replace: "xxx@yyy.zzz" ''(without quotes)''<br />
 +
RegExp<br />
 +
Example: before: "user@example.net", after: "xxx@yyy.zzz"
 +
 
 +
==Remove note numbers==
 +
Find "\[[0-9]+\]" ''(without quotes)''<br />
 +
Replace: "" ''(without quotes)''<br />
 +
RegExp<br />
 +
Example: before: "as listed in his book[1], and other sources[2][3].", after: "as listed in his book, and other sources."
 +
 
 +
==Turn a CSV table into a list of PHP if conditions==
 +
"^([^\,]+)\,([^\,]+)\,([^\,]+)$" ''(without quotes)''<br />
 +
"elseif (vcmp$=="\1") {$v2="\2"; $v3="\3";}" ''(without quotes)''<br />
 +
RegExp<br />
 +
Example: before: "Italy,Rome,Euro", after: elseif (vcmp$=="Italy") {$v2="Rome"; $v3="Euro";}<br />
 +
Note: In this example, there are three values per record, and the CSV separator is a comma (,), you may need to replace it with another character like \t for tab. Remember to change the first "elseif" to "if".
 +
 
 +
==Truncate last value from a MySQL INSERT query==
 +
Find: "(VALUES \(.+), *.+\);" ''(without quotes)''<br />
 +
Replace: "\1);" ''(without quotes)''<br />
 +
RegExp<br />
 +
Example: before: "INSERT INTO table VALUES ('value1','value2','value3');", after: "INSERT INTO table VALUES ('value1','value2');"
 +
 
 +
==Keep only last value in a MySQL INSERT query==
 +
Find: "(VALUES \(.+,)(.+\);)" ''(without quotes)''<br />
 +
Replace: "VALUES (\2" ''(without quotes)''<br />
 +
RegExp<br />
 +
Example: before: "INSERT INTO table VALUES ('value1','value2','value3');", after: "INSERT INTO table VALUES ('value3');"
 +
 
 +
==Turn MySQL fields into empty values==
 +
Find: "`[^`]*`([\,\)\'\"])" ''(without quotes)''<br />
 +
Replace: "<nowiki>''</nowiki>\1" ''(without quotes)''<br />
 +
RegExp<br />
 +
Example: before: "INSERT INTO table (`phone`,`phonecell`,`fax`) VALUES (`phone`,`phonecell`,`fax`);", after: "INSERT INTO table (`phone`,`phonecell`,`fax`) VALUES (<nowiki>'','',''</nowiki>);". (Only the second instance of "(`phone`,`phonecell`,`fax`)" must be selected and search and replace must be applied to such selection).<br />
 +
Note: this is meant to replicate the values in a INSERT query according to the specified fields (and use the empty values as a placeholder): you simply need to copy the fields and past them after VALUES, and then replace them this way.
 +
 
 +
==Wikification: Link all elements of a list==
 +
Find: "(\*)(.*)$" ''(without quotes)''<br />
 +
Replace: "<nowiki>\1[[\2]]</nowiki>" ''(without quotes)''<br />
 +
RegExp<br />
 +
Example: before: "<nowiki>*Example</nowiki>", after: "<nowiki>*[[Example]]</nowiki>"
 +
 
 +
==Wikification: Link all elements of a list, changing the linked text==
 +
Find: "(\*)(.*)$" ''(without quotes)''<br />
 +
Replace: "<nowiki>\1[[\2_(category)|\2]]</nowiki>" ''(without quotes)''<br />
 +
RegExp<br />
 +
Example: before: "<nowiki>*Example</nowiki>", after: "<nowiki>*[[Example_(category)|Example]]</nowiki>"
 +
 
 +
==See also==
 +
*[[Regular Expressions]]

Latest revision as of 09:39, 7 July 2012

Contents

Suppress all empty lines

Find: "^\n" (without quotes)
Replace: "" (without quotes)
RegExp
Note: You may have to repeat it several times to remove all empty lines (until the editor can't find the string to replace), because only the first one of a series of consecutive empty lines will be edited.

Suppress all non-empty lines

Find: "^.+$" (without quotes)
Replace: "" (without quotes)
RegExp

Comment out lines

Find: "^" (without quotes)
Replace: "// " (without quotes)
RegExp
Note: Empty lines will not be affected. This example actually adds a text at the beginning of every non empty line, the comment symbol used ("//") is the one of PHP.

Comment lines

Find: "$" (without quotes)
Replace: " // Comment here" (without quotes)
RegExp
Note: Empty lines will not be affected. This example actually adds a text at the end of every non empty line, the comment symbol used ("//") is the one of PHP.

Join lines

Find: "\n" (without quotes)
Replace: "," (without quotes)
RegExp
Note: Turn separated lines into comma separated values. Empty lines will be affected as well.

Split lines

Find: "," (without quotes)
Replace: "\n" (without quotes)
RegExp
Note: Turn comma separated values into separated lines. Consecutive commas will generate empty lines.

Remove all leading spaces and tabs from every line

Find: "^[ \t]+" (without quotes)
Replace: "" (without quotes)
RegExp
Note: this will obviously remove any indentation

Remove all trailing spaces and tabs from every line

Find: "[ \t]+$" (without quotes)
Replace: "" (without quotes)
RegExp

Remove consecutive spaces

Find: "  +" (without quotes)
Replace: " " (without quotes)
RegExp
Example: before: "this  is an   example", after: "this is an example"

Fix punctuation

Find: "([\.\,\;\:\?\!])([a-zA-Z])" (without quotes)
Replace: "\1 \2" (without quotes)
RegExp
Example: before: "How are you?I'm fine,thanks.", after: "How are you? I'm fine, thanks."
Note: If you want to use this statement in a Wiki context, remove "\:" from the search string, otherwise you may break Wiki metatags, for example [[Category:Example]] will be replaced by [[Category: Example]]

Wrap a tag pair around a text

Find: "(bold)" (without quotes)
Replace: "<b>\1</b>" (without quotes)
RegExp
Example: before: "bold", after: "<b>bold</b>"

Wrap a tag pair around every line

Find: "^(.+)$" (without quotes)
Replace: "<li>\1</li>" (without quotes)
RegExp
Note: this example wraps the tag pair <li></li> every line to create a list of items; you'd probably want to apply changes only to a selected text.

Delete everything inside a tag pair (keeping tags)

Find: "(<delete>).+(</delete>)" (without quotes)
Replace: "\1\2" (without quotes)
RegExp
Note: Tags must be on the same line.
Example: before: "this <delete>is an</delete> example", after: "this <delete></delete> example"

Delete everything inside a tag pair (removing tags too)

Find: "<delete>.+</delete>" (without quotes)
Replace: "" (without quotes)
RegExp
Note: Tags must be on the same line.
Example: before: "this <delete>is an</delete> example", after: "this  example"

Delete all lines containing a given string

Find: "^.*STRING.*$" (without quotes)
Replace: "" (without quotes)
RegExp
Note: Lines will be emptied but not suppressed. See #Suppress all empty lines to suppress empty lines.

Replace only the first occurrence of a character

Find: ";(.*)" (without quotes)
Replace: "|\1" (without quotes)
RegExp
Note: In this example, only the first occurrence of ";" for each line will be replaced with "|".
Example: before: "this;is;an;example", after: "this|is;an;example"

Replace only the last occurrence of a character

Find: "(.*);" (without quotes)
Replace: "\1|" (without quotes)
RegExp
Note: In this example, only the last occurrence of ";" for each line will be replaced with "|".
Example: before: "this;is;an;example", after: "this;is;an|example"

Truncate a string at the first occurrence of a marker (keeping the marker)

Find: "^([^;]*;).*$" (without quotes)
Replace: "\1" (without quotes)
RegExp
Note: In this example, the marker is ";".
Example: before: "this;is;an;example", after: "this;"

Truncate a string at the first occurrence of a marker (removing the marker too)

Find: "^([^;]*);+.*$" (without quotes)
Replace: "\1" (without quotes)
RegExp
Note: In this example, the marker is ";".
Example: before: "this;is;an;example", after: "this"

Truncate a string at the last occurrence of a marker (keeping the marker)

Find: "^(.*;).*$" (without quotes)
Replace: "\1" (without quotes)
RegExp
Note: In this example, the marker is ";".
Example: before: "this;is;an;example", after: "this;is;an;"

Truncate a string at the last occurrence of a marker (removing the marker too)

Find: "^(.*);.*$" (without quotes)
Replace: "\1" (without quotes)
RegExp
Note: In this example, the marker is ";".
Example: before: "this;is;an;example", after: "this;is;an"

Truncate a string up to the first occurrence of a marker (keeping the marker)

Find: "^[^;]*(;.*)$" (without quotes)
Replace: "\1" (without quotes)
RegExp
Note: In this example, the marker is ";".
Example: before: "this;is;an;example", after: ";is;an;example"

Truncate a string up to the first occurrence of a marker (removing the marker too)

Find: "^[^;]*;(.*)$" (without quotes)
Replace: "\1" (without quotes)
RegExp
Note: In this example, the marker is ";".
Example: before: "this;is;an;example", after: "is;an;example"

Truncate a string up to the last occurrence of a marker (keeping the marker)

Find: "^.*(;.*)$" (without quotes)
Replace: "\1" (without quotes)
RegExp
Note: In this example, the marker is ";".
Example: before: "this;is;an;example", after: ";example"

Truncate a string up to the last occurrence of a marker (removing the marker too)

Find: "^.*;(.*)$" (without quotes)
Replace: "\1" (without quotes)
RegExp
Note: In this example, the marker is ";".
Example: before: "this;is;an;example", after: "example"

Turn a list of names last first(s) into first(s) last

Find "^([^ ]*) (.*)$" (without quotes)
Replace: "\2 \1" (without quotes)
RegExp
Note: Names are separated by a space character " ".
Example: before: "Smith John Jack", after: "John Jack Smith"

Remove e-mail addresses

Find "[a-zA-Z0-9\.\-_]+@[a-zA-Z0-9\.\-_]+\.[a-zA-Z0-9\.\-_]+" (without quotes)
Replace: "xxx@yyy.zzz" (without quotes)
RegExp
Example: before: "user@example.net", after: "xxx@yyy.zzz"

Remove note numbers

Find "\[[0-9]+\]" (without quotes)
Replace: "" (without quotes)
RegExp
Example: before: "as listed in his book[1], and other sources[2][3].", after: "as listed in his book, and other sources."

Turn a CSV table into a list of PHP if conditions

"^([^\,]+)\,([^\,]+)\,([^\,]+)$" (without quotes)
"elseif (vcmp$=="\1") {$v2="\2"; $v3="\3";}" (without quotes)
RegExp
Example: before: "Italy,Rome,Euro", after: elseif (vcmp$=="Italy") {$v2="Rome"; $v3="Euro";}
Note: In this example, there are three values per record, and the CSV separator is a comma (,), you may need to replace it with another character like \t for tab. Remember to change the first "elseif" to "if".

Truncate last value from a MySQL INSERT query

Find: "(VALUES \(.+), *.+\);" (without quotes)
Replace: "\1);" (without quotes)
RegExp
Example: before: "INSERT INTO table VALUES ('value1','value2','value3');", after: "INSERT INTO table VALUES ('value1','value2');"

Keep only last value in a MySQL INSERT query

Find: "(VALUES \(.+,)(.+\);)" (without quotes)
Replace: "VALUES (\2" (without quotes)
RegExp
Example: before: "INSERT INTO table VALUES ('value1','value2','value3');", after: "INSERT INTO table VALUES ('value3');"

Turn MySQL fields into empty values

Find: "`[^`]*`([\,\)\'\"])" (without quotes)
Replace: "''\1" (without quotes)
RegExp
Example: before: "INSERT INTO table (`phone`,`phonecell`,`fax`) VALUES (`phone`,`phonecell`,`fax`);", after: "INSERT INTO table (`phone`,`phonecell`,`fax`) VALUES ('','','');". (Only the second instance of "(`phone`,`phonecell`,`fax`)" must be selected and search and replace must be applied to such selection).
Note: this is meant to replicate the values in a INSERT query according to the specified fields (and use the empty values as a placeholder): you simply need to copy the fields and past them after VALUES, and then replace them this way.

Wikification: Link all elements of a list

Find: "(\*)(.*)$" (without quotes)
Replace: "\1[[\2]]" (without quotes)
RegExp
Example: before: "*Example", after: "*[[Example]]"

Wikification: Link all elements of a list, changing the linked text

Find: "(\*)(.*)$" (without quotes)
Replace: "\1[[\2_(category)|\2]]" (without quotes)
RegExp
Example: before: "*Example", after: "*[[Example_(category)|Example]]"

See also