Jump to content
xisto Community
Sign in to follow this  
CarlowOlson

How To Replace Regex On Python?

Recommended Posts

Hello Friends,

 

The Python programming language uses regular expressions for pattern matching. Programmers often use regular expressions to search text for patterns of letters, symbols and numbers. The power of regular expressions comes from the fact that they aren't used to search for something specific, like the word 'dog'; instead, they search for words that match a certain pattern, such as email domain names. But I don't know how to replace Regex on phyton if anybody having any suggestion please share with me.

 

Thanks and Regards

Carlow Olson

Share this post


Link to post
Share on other sites

Regex is like a language in itself. Consider it like SQL. Once you understand regex - you can use it in any language that supports regex to match and operate on data - for example, in python, you haveYour_new_string = old_string_variable.replace(x,y);

Share this post


Link to post
Share on other sites

Hi Guys,

 

1. Open the IDLE text editor that comes bundled with the Python language by clicking on its icon. The IDLE text editor icon is located in the Python directory in your installed programs list (located under All Programs in the Windows Start Menu, and within the Applications folder in OSX). A blank source code file opens in the main editor window.

 

2. Include the 're' module by writing this line at the top of the source code file: import re

 

3. Declare a string and assign some email addresses to it, such as this: emailAddresses = 'William@amail.com, John@bmail.com, Bruce@cmail.com

 

4. Create a regular expression that searches for all possible text permutations in valid email addresses. Regular expressions work by searching for a pattern of characters in a string of text. The pattern you are interested in is any two words joined by an @ symbol. Since email addresses have many valid characters, you want to match all possible characters in each word before and after the @ symbol. This is accomplished with the regular expression [\w\.-], and by adding a + to the end of it, you can repeat this for all the characters. The completed regular expression can be saved to a string like this: regexPattern = r'([\w\.-]+)@([\w\.-]+)'

 

5. Create a regular expression that replaces all of the domain names with "zmail.com." In this regular expression, the backreference character sequence \1 is used to replace the domain of the email addresses. The backreference refers to a location in a regular expression surrounded in parenthesis. By applying the regular expression to the first backreference, you save the email address but discard the old domain name. You can then add a new domain name, like '@zmail.com.' To save this second regular expression to a variable, you can write this: regexReplacement = r'\1@zmail.com'

 

6. Apply the regular expressions to the string containing the email addresses like this: emailAddresses = re.sub

 

7. Print out the email addresses using this line of code. Python 3 uses this syntax for printing: print, while Python 2 uses this syntax: print emailAddresses.

 

8. Run the program by pressing the F5 key. The program output is: William@zmail.com, John@zmail.com, Bruce@zmail.com

 

Thanks and Regards

Tony Mccallum

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.