How to extract the string between 2 characters in same line on python

Related searches
Task

I have a text file with alphanumeric filenames:

\abc1.txt.  \abc2.txt     \abc3.txt     \abcde3.txt
\Zxcv1.txt        \mnbd2.txt     \dhtdv.txt

I need to extract all .txt extensions from the file, which will be in the same line and also different line in the file in python.

Desired Output:
abc1.txt
abc2.txt
abc3.txt
abcde3.txt
Zxcv1.txt
mnbd2.txt
dhtdv.txt

I appreciate your help.

If I were you, I would use regular expressions (regex).

import re

# Open the file with the mode r, which means read the file
with open("text_file.txt", "r") as f:
    # Actually read the content of the file
    file_content = f.read()

# Find everything which matches the given regex code
# This returns a list of the matches
files = re.findall(r"\\(.*?.txt)", file_content)

# Iterate through each item in the list
for file in files:
    # Print the item
    print(file)

Here is the explaination of the regex that I used: https://regex101.com/r/DAPlqM/1

How to extract the substring between two markers?, In python, extracting substring form string can be done using findall method in regular expression ( re ) module. You can find first substring with this function in your code (by character index). Also, you can find what is after a substring. >>> s = '/tmp/10508. Simple python script to extract sub string between two delimiters of a string. Examples of delimiters are brackets, parentheses, braces, single or double quotes etc. You can also customize this code to extract the string between two special characters or two sub strings. Comments on each line of code explains you to easily understand the code.

Try this:

string = r"\abc1.txt. \abc2.txt \abc3.txt \abcde3.txt \Zxcv1.txt \mnbd2.txt \dhtdv.txt"
list = string.split("\\")
print(list)
formatted = "\n".join(list)
print(formatted)

Results:

['', 'abc1.txt. ', 'abc2.txt ', 'abc3.txt ', 'abcde3.txt ', 'Zxcv1.txt ', 'mnbd2.txt ', 'dhtdv.txt']

abc1.txt. 
abc2.txt 
abc3.txt 
abcde3.txt 
Zxcv1.txt 
mnbd2.txt 
dhtdv.txt

How to get the substring between two markers in Python, Code faster with the Kite plugin for your code editor, featuring Line-of-Code Completions and cloudless processing. Extracting the substring between two known marker strings returns the characters between the two markers as a new string. Python offers many ways to substring a string. It is often called ‘slicing’. It follows this template: string[start: end: step] Where, start: The starting index of the substring. The character at this index is included in the substring. If start is not included, it is assumed to equal to 0. end: The terminating index of the substring. The character at this index is NOT included in the

You can use re.findall with a pattern that matches two words delimited by a .:

import re
print('\n'.join(re.findall(r'\w+\.\w+', s)))

Given your input text in variable s, this outputs:

bc1.txt
bc2.txt
bc3.txt
bcde3.txt
Zxcv1.txt
mnbd2.txt
dhtdv.txt

Extract string between two delimiters- GetHowStuff, Simple python script to extract sub string between two delimiters of a string. extract the string between two special characters or two sub strings. Comments on each line of code explains you to easily understand Note: This code is written in Python 2 Excel combine multiple cells into one separated by. A Python program can read a text file using the built-in open() function. For example, the Python 3 program below opens lorem.txt for reading in text mode, reads the contents into a string variable named contents, closes the file, and prints the data.

Extract text from a file between two markers, For simple cases, this could also be solved with a regular expression. Extract Values between two strings in a text file using python � printing lines between start� Method #2 : Using regex( findall() ) In the cases which contain all the special characters and punctuation marks, as discussed above, the conventional method of finding words in string using split can fail and hence requires regular expressions to perform this task.

In Python, in a string, how can you grab all the text between two , In Python, in a string, how can you grab all the text between two words in a How do you change one character in a string in Python? string of key=value pairs (eg. string = "a=1;b=2;c=3;") into dictionary in Python How do I search a text file for a specific string in Python, and if it exists, how do I print the line with the string� Each file contains two unique words ()alpha and bravo and i want to extract the text between these unique words and write it to a different file. Manually i can CTRL+F for the two words and copy the text between, i just want to know how to do this using a program (preferably Python) for many files.

Given your sample input, it looks like every line begins and ends with brackets. In which case, forget regexps, this is trivial: for line in whatever: contents = line.strip()[1:-1] (I've added the strip in case your line source is leaving the newlines in, or there are invisible spaces after the closing bracket in your input. If it's not

Comments
  • What have you tried so far?
  • for more exact matching files = re.findall(r"\(.*?.txt\b)", file_content) will help
  • @Justin Remember that you have to escape the backslash. So this is better: files = re.findall(r"\\(.*?.txt\b)", file_content)