How do I slice a string in python based on the text, not the letter number?

wellslight

I am trying to automate some document editing and wondering if there's a way to program this to simplify it? I need to remove large portions of text that are in the center of a user input that varies each time but is surrounded by constants. For example, the user input would be a copy/paste in this format:

"A: Test Name Here

B: Test Date Here

C: Long details that are unnecessary here.

D: Summary here"

I would like to cut out C all the way up to the start of D to get:

"A: Test Name Here

B: Test Date Here

D: Summary here"

I want to do something like delete ["C" : "D" -1] and save the rest of the text. In the actual documents "C" is a more specific unique word and wouldn't actually delete every letter c in the text.

Seems simple but I can't figure it out. I appreciate any help!

Czaporka

You could use re.sub:

import re

s = """\
A: Test Name Here
B: Test Date Here
C: Long details that are unnecessary here.
D: Summary here"""

print(s)
print("---------")
print(re.sub("C:[\s\S]*?(?=D:)", "", s))

Output:

A: Test Name Here
B: Test Date Here
C: Long details that are unnecessary here.
D: Summary here
---------
A: Test Name Here
B: Test Date Here
D: Summary here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I count the number of occurrences in a string based on character letter?

From Dev

How do I identify in a string that a letter has a number beside?

From Dev

How do I find a combination of a letter a number in a VBA string?

From Dev

How do I check if a Java String contains at least one capital letter, lowercase letter, and number?

From Dev

how do i remove a letter from string

From Dev

How do I slice a string if whitespace is detected?

From Dev

How do i check string to contain only letter(Uppercase & Lowercase) and at least one number only

From Dev

How do i check string to contain only letter(Uppercase & Lowercase) and at least one number only

From Dev

How do I parse a string consisting of a letter and number idiomatically and efficiently in Rust?

From Dev

How do I count the number of words in a text (string)?

From Dev

How do I count the number of words in a text (string)?

From Dev

How do I check if a std::string, containing utf8 text, starts with an uppercase letter in Windows?

From Dev

How do you return the last letter in a string of text in an adjacent column?

From Dev

How do I slice a pandas dataframe based on a condition?

From Dev

How do I isolate a number from a string in python(django)?

From Dev

How do I check if a string starts with a negative number and a float? [python]

From Dev

How do I select text except for the first letter?

From Dev

How to slice off a certain number of bytes from a string in Python?

From Dev

If ASCII String Contains Letter or Number, Then Return Text

From Dev

How can I find the number of 8 letter words that do not contain the letter "e", using the grep command?

From Dev

How do I make a single letter in a string uppercase

From Dev

How do I add a specific letter to a variable and then make it a string

From Dev

How do I create a method that capitalizes the first letter of a string in java?

From Dev

How do I grab each letter of a string in android?

From Dev

How do i make random letter from a string?

From Dev

How do i invert every odd letter in a string?

From Java

How do I blit text letter by letter in pygame? (like in those retro RPG games)

From Dev

How do I capitalized any two letter word in a string in addition to the first letter of any word?

From Dev

How do Slice a string into pairs

Related Related

  1. 1

    How can I count the number of occurrences in a string based on character letter?

  2. 2

    How do I identify in a string that a letter has a number beside?

  3. 3

    How do I find a combination of a letter a number in a VBA string?

  4. 4

    How do I check if a Java String contains at least one capital letter, lowercase letter, and number?

  5. 5

    how do i remove a letter from string

  6. 6

    How do I slice a string if whitespace is detected?

  7. 7

    How do i check string to contain only letter(Uppercase & Lowercase) and at least one number only

  8. 8

    How do i check string to contain only letter(Uppercase & Lowercase) and at least one number only

  9. 9

    How do I parse a string consisting of a letter and number idiomatically and efficiently in Rust?

  10. 10

    How do I count the number of words in a text (string)?

  11. 11

    How do I count the number of words in a text (string)?

  12. 12

    How do I check if a std::string, containing utf8 text, starts with an uppercase letter in Windows?

  13. 13

    How do you return the last letter in a string of text in an adjacent column?

  14. 14

    How do I slice a pandas dataframe based on a condition?

  15. 15

    How do I isolate a number from a string in python(django)?

  16. 16

    How do I check if a string starts with a negative number and a float? [python]

  17. 17

    How do I select text except for the first letter?

  18. 18

    How to slice off a certain number of bytes from a string in Python?

  19. 19

    If ASCII String Contains Letter or Number, Then Return Text

  20. 20

    How can I find the number of 8 letter words that do not contain the letter "e", using the grep command?

  21. 21

    How do I make a single letter in a string uppercase

  22. 22

    How do I add a specific letter to a variable and then make it a string

  23. 23

    How do I create a method that capitalizes the first letter of a string in java?

  24. 24

    How do I grab each letter of a string in android?

  25. 25

    How do i make random letter from a string?

  26. 26

    How do i invert every odd letter in a string?

  27. 27

    How do I blit text letter by letter in pygame? (like in those retro RPG games)

  28. 28

    How do I capitalized any two letter word in a string in addition to the first letter of any word?

  29. 29

    How do Slice a string into pairs

HotTag

Archive