Regular Expression (RE)

They can be used with string operations.
Using this, we specify the rules for the set of possible strings that we want to match (Searching for patterns in another string).
Note : its finds FIRST instance of that pattern
We can also use REs to modify a string or to split it apart in various ways.
To use this ,

Import re

We need to import the above module.

Functions in RE module

Search

Syntax:

match = re.search(pat, text

Here, Match > match object
Re. > used to call function in module RE

Ex:
match = re.search(‘iig’,'called piiiig')

Now to see the result , we can use

Match.group()

>>> m.group()
'iig'

It will show us the result of the match.

A function for this can be –

def Find(pat,text):
m =re.search(pat,text)
if m:print (m.group())
else : print('not found')

Now we see some matches for function ‘Find’

>>> Find('ig','i am a piiiig')
ig
>>> Find('...g','i am a piiiig')
iiig
>>> Find('p...g','i am a piiiig')
not found
>>> Find('p....g','i am a piiiig')
piiiig
>>> Find('p....g','i am a piiiigand no ghgg')
piiiig
>>> Find('g..g','i am a piiiigand no ghgg')
ghgg
>>> Find('g..g','i am a piiiigand no gtrg ghgg')
gtrg
>>> Find('g..g','i am a piiiigand no gtrg ghgg')
gtrg
>>> Find('g..g','i am a piiiigand no gbng gtrg ghgg')
Gbng

Note : As we can see, only first instance is displayed here.

The Dot operator (.) is like blanks which can be anything.

Raw string - >>> Find(r'p\.i','i am a p.iiiigand no gbng gtrg ghgg')
p.i


Slash ( \ )

Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning.

>>> Find(r'\\','i am a p.\iiiigand no gbng gtrg ghgg')
\

>>>Find(r'\\ii..','i am a p.\iiiigand no gbng gtrg ghgg')
\iiii

>>> find('\*','hi*+ther tether')
*
>>> find('\+','hi*+ther tether')
+


\w operator

Finds letters after a special symbol
I mean \w will find 1.
\w\w will find 2 .

Ex: >>> find(r':\w\w','song :hi re hi')
:hi
The no. of letters can be any number .

Ex2: >>> find(r':\w\w','song :hi re hi')
:hi

It finds the 1st 3 letters of the sentence.

Ex3: >>> find(r';\w\w\w','song ;hithere hi again')
;hit

As you can see, it can match any part after any symbol.

Ex4: This is another way which will show all the letters till we reach a blank.

>>> match = re.search(':\w+','123edit :the fucking line')
>>> match.group()
':the'

Now if we want to see the rest of the sentence from the search point .-

>>> match = re.search(':\w.+','123edit :the fucking line')
>>> match.group()
':the fucking line'

The dot sign means all after that point.

Now take this example . If we have an email address like - arin.thearc@gmail.com

>>> match = re.search('\w+@\w+','arin.thearc@gmail.com')
>>> match.group()
'thearc@gmail'

Using above method will not display anything after dot(.) sign . So ,

>>> match = re.search('[\w.]+@[\w.]+','arin.thearc@gmail.com')
>>> match.group()
'arin.thearc@gmail.com'

Here , the brackets are used to allow a special symbol to be allowed to be read .

\d operator

Digit finder. Finds digits occurrence anywhere.

>>> find(r'\d\d\d','song :re 123xxx')
123

>>> find(r'\d\d\d','song :re sss123xxx')
123

>>> match = re.search('\d','123edit the fucking line')
>>> match.group()
'1'

>>> match = re.search('\d+','123edit the fucking line')
>>> match.group()
'123'
White space charatcters

Blank space , tabs all come under white space .

A ‘\s’ detects every one.

>>> find(r'\d\s\d\s\d','1 2 3')
1 2 3

Non-White space charatcters

A capital s ‘S’ is for non-white space.

>>> find(r':\S+','blabuuu4564&======booo :oyehoye puttar' )
:oyehoye

+ sign

Causes the resulting RE to match 1 or more repetitions of the preceding RE

>>> find(r'\d\s+\d\s+\d','1 2 3')
1 2 3

>>> find(r':\w+','bla buuu booo :oyehoye' )
:oyehoye

The above code will match any length after the colon .

>>> find(r'\w+','blabuuu booo :oyehoye' )
Blabuuu

The above code will match the 1st word completely.

Note : anything can be a woed , but not space.
See the code below

>>> find(r'\w+','blabuuu4564 booo :oyehoye' )
blabuuu4564

>>> find('.+','blabuuu4564&booo :oyehoye' )
blabuuu4564&booo :oyehoye

So what the + does is , its greedy . It goes as far as it can, and then it stops.

Dot operator( . )

In the default mode, this matches any character except a newline

>>> find('.','blabuuu4564&booo :oyehoye' )
b

>>> find('.+','blabuuu4564&booo :oyehoye' )
blabuuu4564&booo :oyehoye


This is a work in progress. i will add to it gradually .

This seems like a direct-copy paste of http://letshare.it/blog/regular-expression-beginner.html

Do you own that content?

This work took me a week to come this far . I took a look at the page you to told me . Sem like somebody has directly copy pasted my work . Wel he cant construct the rest of the doc because , the doc i posted is not complete yet . Il be posting its continuation in a week as im studying that regularly. But thanks for warning me .

Oh and one more thing......look at that link ouposted carefully......you will find my email adress too......thearc@gmail.com..........That idiot could have atleast changed that........really pathetic .............i cant believe it.........

I think this site automatically crawls the web and publishes forum posts at random.

hello Daniweb users
we are the team of letshare posters we are sorry for sharing your post but it is a system that crawls important pages from the web and publish them with a link back to the orginal post we work just like google if you think that we harm your work please tell us and we will remove the post we work very well to share the best posts in the web we don't spam and we are not aim to earn money using the efforts of others so please reply on this comment to to remove the post if you want

thanks for understanding

Sincerly letshare team

hello Daniweb users
we are the team of letshare posters we are sorry for sharing your post but it is a system that crawls important pages from the web and publish them with a link back to the orginal post we work just like google if you think that we harm your work please tell us and we will remove the post we work very well to share the best posts in the web we don't spam and we are not aim to earn money using the efforts of others so please reply on this comment to to remove the post if you want

thanks for understanding

Sincerly letshare team

Its OK boss.....I posted it so that people can read and make good use of it.......Knowledge is for sharing...........I am happy people find it usefull......Thanks for replying.....

This question has already been answered. Start a new discussion instead.