Reputation: 31
Can anyone please provide me the regex code for printing only the first line of the data in the text file??? I am using spyder
i have tried may solutions but it prints all my data in every line ...last one helped me but it chose two lines. i just want the first line of my text file only till it encounters line break or till the text starts from next line.
import re
def getname(s):
nameregex=re.findall(r'^.*?[\.!\?](?:\s|$)',line)
if len(nameregex)!=0:
print(nameregex)
s = open('yesno.txt')
for line in s:
getname(s)
In the output i am getting first two lines. Basically i am trying to print the company name only which is mostly in the first line.
Upvotes: 1
Views: 2626
Reputation: 627607
Read the file into a variable using read()
and use re.search
to get the match:
import re
def getname(s):
nameregex=re.search(r'^.*?[.!?](?!\S)', s) # Run search with regex
if nameregex: # If there is a match
print(nameregex.group()) # Get Group 0 - whole match - value
s = open('yesno.txt', 'r') # Open file handle to read it
contents = s.read() # Get all file contents
getname(contents) # Run the getname method with the contents
The regex is a bit modified to avoid the whitespace at the end. See details:
^
- start of the string.*?
- any 0 or more chars other than line break chars, as few as possible[.!?]
- .
, !
or ?
char(?!\S)
- there must be a whitespace or end of string here.See the regex graph:
Upvotes: 1