Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
77 views

Python

The document discusses using regular expressions (re module) in Python to search and find patterns in strings. It provides examples of using re functions like findall, search, split, sub, and flags like ^, $, *, +, {}, | to match patterns at the start, end or within strings. Metacharacters and their usage are also explained to match characters, digits, words or whitespace.

Uploaded by

babjeereddy
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Python

The document discusses using regular expressions (re module) in Python to search and find patterns in strings. It provides examples of using re functions like findall, search, split, sub, and flags like ^, $, *, +, {}, | to match patterns at the start, end or within strings. Metacharacters and their usage are also explained to match characters, digits, words or whitespace.

Uploaded by

babjeereddy
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

python -m pip install cx_Oracle

import cx_Oracle

conn =cx_Oracle.connect('training/ADMIN123@LOCALHOST')
cursor =conn.cursor();
cursor.execute("Create table student_t(Student_id number,student_name
varchar2(20),city varchar2(20))")
print("Create table Successfull")
cursor.close()
conn.close()

-------------------------------------------------------------
import cx_Oracle

conn =cx_Oracle.connect('training/ADMIN123@LOCALHOST')
cursor =conn.cursor();
cursor.execute("insert into student_t values(100,'babjee','chennai')")
print("Create table Successfull")
cursor.close()
conn.close()

import cx_Oracle
conn =cx_Oracle.connect("training","ADMIN123",'localhost/xe')
sql ="select empno,ename,job,sal from emp"
column_length=[6,10,9,8]
cursor=conn.cursor()
for row in cursor.execute(sql):
for i in range(len(row)):

print(str(row[i]).ljust(column_length[i]), end='')
print()
cursor.clouse()

----------------------------------------------------------------
from openpyxl import load_workbook
wb =load_workbook("d:/demo/emp.xlsx")
sheet =wb.active
for row in sheet.iter_rows(min_row=1, min_col=1, max_row=4, max_col=2):
for cell in row:
print(cell.value, end=" ")
print()
----------------------------------------------------------------
The re module offers a set of functions that allows us to search a string for a
match:

findall Returns a list containing all matches


search Returns a Match object if there is a match anywhere in the string
split Returns a list where the string has been split at each match
sub Replaces one or many matches with a string

---------------------------------------------------------------
Metacharacters
Metacharacters are characters with a special meaning:

[] A set of characters "[a-m]"


\ Signals a special sequence (can also be used to escape special characters)
"\d"
. Any character (except newline character) "he..o"
^ Starts with "^hello"
$ Ends with "world$"
* Zero or more occurrences "aix*"
+ One or more occurrences "aix+"
{} Exactly the specified number of occurrences "al{2}"
| Either or "falls|stays"

Find all lower case characters alphabetically between "a" and "m":

import re

txt = "The rain in Spain"

x = re.findall("[a-m]", txt)
print(x)
----------------------------------------------------------------

#Find all digit characters:

import re

txt = "That will be 59 dollars"

x = re.findall("\d", txt)
print(x)
----------------------------------------------------------------
#Search for a sequence that starts with "he", followed by two (any) characters, and
an "o":

import re

txt = "hello world"


x = re.findall("he..o", txt)
print(x)
---------------------------------------------------------------
#Check if the string starts with 'hello':

import re

txt = "hello world"


x = re.findall("^hello", txt)
if x:
print("Yes, the string starts with 'hello'")
else:
print("No match")
----------------------------------------------------------------
#Check if the string contains "ai" followed by 0 or more "x" characters:

import re
txt = "The rain in Spain falls mainly in the plain!"
x = re.findall("aix*", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")

---------------------------------------------------------------
#Check if the string contains "ai" followed by 1 or more "x" characters:

import re
txt = "The rain in Spain falls mainly in the plain!"
x = re.findall("aix+", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
-------------------------------------------------------------
#Check if the string contains "a" followed by exactly two "l" characters:

import re

txt = "The rain in Spain falls mainly in the plain!"


x = re.findall("al{2}", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
---------------------------------------------------------------
#Check if the string contains either "falls" or "stays":
import re
txt = "The rain in Spain falls mainly in the plain!"
x = re.findall("falls|stays", txt)
print(x)
if x:
print("Yes, there is at least one match!")
else:
print("No match")
----------------------------------------------------------------
\A Returns a match if the specified characters are at the beginning of the
string "\AThe"

\b Returns a match where the specified characters are at the beginning or at the
end of a word

\B Returns a match where the specified characters are present, but NOT at the
beginning (or at the end) of a word
\d Returns a match where the string contains digits (numbers from 0-9) "\d"

\D Returns a match where the string DOES NOT contain digits "\D"
\s Returns a match where the string contains a white space character "\s"

\S Returns a match where the string DOES NOT contain a white space character
"\S"

\w Returns a match where the string contains any word characters (characters
from a to Z, digits from 0-9, and the underscore _ character) "\w"
\W Returns a match where the string DOES NOT contain any word characters "\W"

\Z Returns a match if the specified characters are at the end of the string
"Spain\Z"

import re

txt = "The rain in Spain"

x = re.findall("\AThe", txt)
print(x)

if x:
print("Yes, there is a match!")
else:
print("No match")

You might also like