CarlowOlson 0 Report post Posted October 16, 2012 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
iGuest 3 Report post Posted October 17, 2012 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
TonyMccallum 1 Report post Posted October 18, 2012 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