Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python RegEx Searching for an occurrence of the pattern

# A Python program to demonstrate working of re.match().
import re

# Lets use a regular expression to match a date string in the form of Month name followed by day number
regex = r"([a-zA-Z]+) (d+)"

match = re.search(regex, "I was born on march 5")

if match != None:

	# We reach here when the expression "([a-zA-Z]+) (d+)" matches the date string. This will print [14, 21), since it matches at index 14 and ends at 21.
	print ("Match at index %s, %s" % (match.start(), match.end()))

	# We us group() method to get all the matches and captured groups. The groups contain the matched values. In particular: match.group(0) always returns the fully matched string match.group(1) match.group(2), ... return the capture groups in order from left to right in the input string match.group() is equivalent to match.group(0) So this will print "march 5"
	print ("Full match: %s" % (match.group(0)))

	# So this will print "march"
	print ("Month: %s" % (match.group(1)))

	# So this will print "5"
	print ("Day: %s" % (match.group(2)))

else:
	print ("The regex pattern does not match.")
Comment

python regex find single character

# credit to Stack Overflow user in the source link
# finds isolated alphabetical characters
import re
s = "fish oil B stack peanut c <b>"

words = re.finditer('S+', s)
has_alpha = re.compile(r'[a-zA-Z]').search
for word in words:
    if len(word.group()) == 1 and has_alpha(word.group()):
        print(word.start()) # prints the index inside the string
Comment

PREVIOUS NEXT
Code Example
Python :: rename files in python 
Python :: foreign key django createview 
Python :: how to inheritance in python 
Python :: plotly create plot 
Python :: python greater than dunder 
Python :: Python Tkinter CheckButton Widget 
Python :: theme_use() tkinter theme usage 
Python :: python dict to string 
Python :: import tkinter module in python file 
Python :: python replace in string 
Python :: recall at k calculate python 
Python :: convert all columns to float pandas 
Python :: assigning crs using python pyproj 
Python :: how to print tables using python 
Python :: Setting spacing (minor) between ticks in matplotlib 
Python :: how to get the end of a item in a python array 
Python :: Access Google Photo API with Python using google-api-python-client 
Python :: Example pandas.read_hfd5() 
Python :: how to save python-pptx 
Python :: items of list 
Python :: python telegram bot async 
Python :: how to access a file from root folder in python project 
Python :: python dictionary with dot notation 
Python :: tail a log file with python 
Python :: flattern in keras 
Python :: pandas series map 
Python :: python how to invert an array 
Python :: remove element from list python by value 
Python :: how to put space in between list item in python 
Python :: NumPy bitwise_and Syntax 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =