Removing Text Between 2 Strings

Python - Remove text between two string of the same line

You can read the lines and store them in a list:

with open('file.txt', 'r') as f:
text = [line for line in f]

Then you can check whether the line contains 'AS' and '$$' and if it does you can write out 'AS $$', otherwise you write out the original line:

with open('txt.txt', 'w') as f:
for t in text:
if 'AS' in t and '$$' in t:
f.write('AS $$\n')
else:
f.write(t)

notepad++ remove text between two string using regular expression

You may use

(VALUES \().*?,\s*(N')

and replace with $1$2. Note that in case the part of string to be removed can contain line breaks, enable the . matches newline. If the N and VALUES must be matched only when in ALLCAPS, make sure the Match case option is checked.

Pattern details

  • (VALUES \() - Group 1 (later referred with $1 from the replacement pattern): a literal substring VALUES (
  • .*? - any 0+ chars, as few as possible, up to the leftmost occurrence of the sunsequent subpatterns
  • ,\s* - a comma and 0+ whitespaces (use \h instead of \s to only match horizontal whitespace chars)
  • (N') - Group 2 (later referred with $2 from the replacement pattern): a literal substring N'.

enter image description here

Remove text between two strings Applescript

Do the following:

  • Get the text before startText (text1)
  • Get the text after endText (text2)
  • Concatenate text1 & startText & endText & text2

set startText to "<target object=\"3097595957\" channel=\"./2/100\" name=\"\"/>"
set endText to "<target object=\"3097805072\" channel=\"./2/100\" name=\"\"/>"

set theFile to POSIX path of newNameFull
set theContent to read theFile as «class utf8»

set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to startText
set text1 to text item 1 of theContent
set AppleScript's text item delimiters to endText
set text2 to text item 2 of theContent
set AppleScript's text item delimiters to ASTID
set trimmedText to text1 & startText & endText & text2

The result is in the variable trimmedText. You might save the text back to disk.

Deleting text between two strings in php using preg_replace

However, when the string contains multiple "CROPEND" it crops everything from the CROPSTART to the last CROPEND.

This is because your + operator is greedy - it won't stop at the first instance of CROPEND and continue until it encounters the last instance.

You can use a non-greedy version of the + operator simply by appending a ? after it:

preg_replace('/CROPSTART[\s\S]+?CROPEND/', '', $string);


Related Topics



Leave a reply



Submit