Presentation is loading. Please wait.

Presentation is loading. Please wait.

SlideSet #19: Regular expressions SY306 Web and Databases for Cyber Operations.

Similar presentations


Presentation on theme: "SlideSet #19: Regular expressions SY306 Web and Databases for Cyber Operations."— Presentation transcript:

1 SlideSet #19: Regular expressions SY306 Web and Databases for Cyber Operations

2 Regular Expressions Examples: –re.search(r’ab’, mystring) –re.match(r’ab’, mystring) –re.search(r’^ a (a|b|c)* b $’, mystring, re.X) Precedence:

3 Some Regular Expression Quantifiers and Metacharactes Quantifier/SymbolMatches {n} {m,n} {n,} + * ? ^ $ \b \w \d \s \S [abc].

4 Regular Expressions - Examples #!/usr/bin/env python3 import re myString = "Now is is the time"; print ("Test string is ‘” + myString + “'"); if re.search(r’Now’, myString) : print (‘Search 1 success’) searchObj = re.search(r’^Now’, myString) if searchObj: print (‘Search 2 success’) searchObj = re.search(r’Now$’, myString) if searchObj: print (‘Search 3 success’) searchObj = re.search(r’ \b ( \w+ ow ) \b’, myString, re.X) if searchObj: print (‘Search 4 success: ’ + searchObj.group(1)) searchObj = re.search(r’ \b ( \w+ ) \s ( \1)\b’, myString, re.X) if searchObj: print (‘Search 5 success: ’ + searchObj.group(1) + “ ” + searchObj.group(2)) set19_reExample.py

5 Search and Replace newstring = re.sub(pattern, replacement, string, max=0) Example (replace aa with bb): line = “This string has aa here and aa here” newline = re.sub(r’aa’,’bb’,line)

6 Exercise #1 Write the expression to replace one or more newline characters in a string with “&&”. Make it work for both Unix (\n) and Windows (\r\n)

7 Uses for Regular Expressions Input validation Input sanitization


Download ppt "SlideSet #19: Regular expressions SY306 Web and Databases for Cyber Operations."

Similar presentations


Ads by Google