Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python string startswith regex

import re
if re.match(r'^hello', somestring):
    # do stuff
Comment

startswith and re.search^ in python

# This is how we use startswith
# Open a file
hand = open('Path and the name of the file')
count1 = 0
for line in hand:
    # remove new line (/n) at the end of the line
    line = line.rstrip()
    # If there is the 'sub string' at the beginning of the line it will give True
    if line.startswith('Substring'):
        count1 += 1
        print(count1, line)
# Close the file
hand.close()

# This is how we use re.search with carrot sign ^
# This means match the beginning of the line
import re
count2 = 0
hand2 = open('Path and the name of the file')
for line2 in hand2:
    # remove new line (/n) at the end of the line
    line2 = line2.rstrip()
    # before the substring you need to type ^
    if re.search('^substring', line2):
        count2 += 1
        print(count2, line2)
# Close the file
hand2.close()
Comment

PREVIOUS NEXT
Code Example
Python :: how to download a .xlsx file from google colab 
Python :: python filter data from list 
Python :: sqlalchemy filter between dates 
Python :: python check if dataframe series contains string 
Python :: python pow 
Python :: python thread function 
Python :: pyspark dataframe to parquet 
Python :: jupyter tabnine for jupyter notebook 
Python :: python series 
Python :: escape character in python 
Python :: openpyxl check if worksheet exists 
Python :: hstack 
Python :: python subtract every element in list 
Python :: Game of Piles Version 2 codechef solution 
Python :: sort rows by values dataframe 
Python :: Compute the 2d histogram of x and y. 
Python :: ocaml add element to end of list 
Python :: calculate days between two dates using python 
Python :: geopandas stack or concatenate dataframe together 
Python :: Iterate through characters of a string in python 
Python :: how to get count by using group by in python 
Python :: merging df vertically 
Python :: compare dates python 
Python :: python cholesky 
Python :: django authenticate 
Python :: enumerate string pythonm 
Python :: python dataclass 
Python :: selenium set chrome executable path 
Python :: int to alphabet letter python 
Python :: python json string indices must be integers 
ADD CONTENT
Topic
Content
Source link
Name
7+8 =