SlideShare una empresa de Scribd logo
1 de 14
Regular Expression in Action Brief overview of Regular Expression building blocks and tools with a practical example Muhammad Sheraz Siddiqi http://www.sherazsiddiqi.com/
What are Regular Expressions Tools to learn Literal characters and Special characters Build blocks of Regular Expressions    Grouping and Backreferences Unicode characters in regular expressions Regex Matching Modes Lookarounds Parse a log file… This Presentation… http://www.sherazsiddiqi.com/
Regular expressions provide a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters. What are Regular Expressions? http://www.sherazsiddiqi.com/
The Regex Coach is a graphical application for Windows which can be used to experiment with regular expressions interactively. http://weitz.de/regex-coach/ Notepad++ is a text editor that has support of find and replace using Regular Expressions. http://notepad-plus-plus.org/ Web based Regular Expressions tester. http://www.regular-expressions.info/javascriptexample.html Tools to learn? http://www.sherazsiddiqi.com/
The most basic regular expression consists of a literal which behaves just like string matching. For e.g. catwill match cat in About cats and dogs. Special characters known as meta characters needs to be escaped with a in regular expressions if they are used as part of a literal:  dogswill match dogs. in About cats and dogs. Meta characters are: [       ^    $    .    |    ?    *    +    (    )    { Literal and Special characters http://www.sherazsiddiqi.com/
With a "character class", also called "character set", you can tell the regex engine to match only one out of several characters. For e.g. gr[ae]ywill match grey and gray both. Ranges can be specified using dash. For e.g.  [0-9]will match any digit from 0 to 9. [0-9a-fA-F]will match any single hexadecimal digit. Caret after the opening square bracket will negate the character class. The result is that the character class will match any character that is not in the character class. For e.g. [^0-9]will match any thing except number. q[^u]will not match Iraq  but it will match Iraq is a country Character Classes and Shorthands http://www.sherazsiddiqi.com/
Meta characters works fine without escaping in Character classes. For e.g. [+*]is a valid expression and match either * or +. There are some pre-defined character classes known as short hand character classes: stands for[A-Za-z0-9_] stands for[ ] stands for[0-9] If a character class is repeated by using the ?, * or + operators, the entire character class will be repeated, and not just the character that it matched. For e.g. [0-9]+ can match 837 as well as 222 ([0-9])+ will match 222 but not 837. Character Classes and Shorthands http://www.sherazsiddiqi.com/
The famous dot “.” operator matches anything. For e.g. a.bwill match abb, aab, a+betc. ^ and $ are used to match start and end of regular expressions. For e.g. ^My.*$will match anything starting with My and ending with a dot. Pipe operator is used to match a string against either its left or the right part. For e.g. (cat|dog) can match both cat or dog.  Question: If the expression is Get|GetValue|Set|SetValueand string isSetValue. What will this match and why? What if the expression becomes Get(Value)?|Set(Value)? * or {0,} and+ or {1,} are used to control repititions. Building blocks of Regular Exp. http://www.sherazsiddiqi.com/
Round brackets besides grouping part of a regular expression together, also create a "backreference". A backreference stores the matching part of the string matched by the part of the regular expression inside the parentheses. For e.g. ([0-9])+ will match 222 but not 837. If backreference are not required, you can optimize this regular expression Set(?:Value)? Backreferences can be used in expressions itself or in replacement text. For e.g. <([A-Za-z][A-Za-z0-9]*)>.*</> will match matching opening and closing tags. Grouping and Backreferences http://www.sherazsiddiqi.com/
Unicode characters can be used as xxxx in regular expressions. For e.g. عطاری cat be matched in an expression as: 063906370627063106cc Unicode characters in Regular Exp. http://www.sherazsiddiqi.com/
/i makes the regex match case insensitive.  [A-Z] will match A and a with this modifier. /s enables "single-line mode". In this mode, the dot matches newlines as well.  .* will match sherazattari with this modifier. /m enables "multi-line mode". In this mode, the caret and dollar match before and after newlines in the subject string. .* will match only sherazin sherazattari with this modifier. /x enables "free-spacing mode". In this mode, whitespace between regex tokens is ignored, and an unescaped # starts a comment.  #sheraz.* will match only sherazin with this modifier. Regular Exp. Matching Modes http://www.sherazsiddiqi.com/
A conditional is a special construct that will first evaluate a lookaround, and then execute one sub-regex if the lookaround succeeds, and another sub-regex if the lookaround fails. Example of Positive lookahead is: q(?=uv*)will match q in quvvvv and qu. Example of Negative lookahead is: q(?!uv*)will match q not followed by u and uv. Example of Positive lookbehind is: (?<=b)awill match a prefixed by b like ba. Example of Negative lookbehind is: (?<!b)awill match a not prefixed by b like ca and da etc. Lookarounds with Conditions… http://www.sherazsiddiqi.com/
Example1:: I have an access log (access.log) file of Helix DNA server. I want to calculate how many times each content is access and update download and listen count of each content in the database.  Exp: ^(.*)asxgen/Data/Naat/Download(.*)/(+)(mp3|rm)(.*)$ Replace: UPDATE DB.TBL set col=col + COUNT where id=; Example2:: I have application generated log (applog.txt) file of a web application. I want to fetch required information from relevant rows. In order to remove irrelevant rows: Exp: ^(?!((.*)ID:(.*)Status:(.*))).*$ Replace: Empty string Parse a log file… http://www.sherazsiddiqi.com/
Questions Please….. http://www.sherazsiddiqi.com/ Thank you for being here… Most of the content is taken from: http://www.regular-expressions.info/ me@sherazsiddiqi.com

Más contenido relacionado

Último

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 

Último (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 

Destacado

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Destacado (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Regular Expressions In Action

  • 1. Regular Expression in Action Brief overview of Regular Expression building blocks and tools with a practical example Muhammad Sheraz Siddiqi http://www.sherazsiddiqi.com/
  • 2. What are Regular Expressions Tools to learn Literal characters and Special characters Build blocks of Regular Expressions   Grouping and Backreferences Unicode characters in regular expressions Regex Matching Modes Lookarounds Parse a log file… This Presentation… http://www.sherazsiddiqi.com/
  • 3. Regular expressions provide a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters. What are Regular Expressions? http://www.sherazsiddiqi.com/
  • 4. The Regex Coach is a graphical application for Windows which can be used to experiment with regular expressions interactively. http://weitz.de/regex-coach/ Notepad++ is a text editor that has support of find and replace using Regular Expressions. http://notepad-plus-plus.org/ Web based Regular Expressions tester. http://www.regular-expressions.info/javascriptexample.html Tools to learn? http://www.sherazsiddiqi.com/
  • 5. The most basic regular expression consists of a literal which behaves just like string matching. For e.g. catwill match cat in About cats and dogs. Special characters known as meta characters needs to be escaped with a in regular expressions if they are used as part of a literal: dogswill match dogs. in About cats and dogs. Meta characters are: [ ^ $ . | ? * + ( ) { Literal and Special characters http://www.sherazsiddiqi.com/
  • 6. With a "character class", also called "character set", you can tell the regex engine to match only one out of several characters. For e.g. gr[ae]ywill match grey and gray both. Ranges can be specified using dash. For e.g. [0-9]will match any digit from 0 to 9. [0-9a-fA-F]will match any single hexadecimal digit. Caret after the opening square bracket will negate the character class. The result is that the character class will match any character that is not in the character class. For e.g. [^0-9]will match any thing except number. q[^u]will not match Iraq but it will match Iraq is a country Character Classes and Shorthands http://www.sherazsiddiqi.com/
  • 7. Meta characters works fine without escaping in Character classes. For e.g. [+*]is a valid expression and match either * or +. There are some pre-defined character classes known as short hand character classes: stands for[A-Za-z0-9_] stands for[ ] stands for[0-9] If a character class is repeated by using the ?, * or + operators, the entire character class will be repeated, and not just the character that it matched. For e.g. [0-9]+ can match 837 as well as 222 ([0-9])+ will match 222 but not 837. Character Classes and Shorthands http://www.sherazsiddiqi.com/
  • 8. The famous dot “.” operator matches anything. For e.g. a.bwill match abb, aab, a+betc. ^ and $ are used to match start and end of regular expressions. For e.g. ^My.*$will match anything starting with My and ending with a dot. Pipe operator is used to match a string against either its left or the right part. For e.g. (cat|dog) can match both cat or dog. Question: If the expression is Get|GetValue|Set|SetValueand string isSetValue. What will this match and why? What if the expression becomes Get(Value)?|Set(Value)? * or {0,} and+ or {1,} are used to control repititions. Building blocks of Regular Exp. http://www.sherazsiddiqi.com/
  • 9. Round brackets besides grouping part of a regular expression together, also create a "backreference". A backreference stores the matching part of the string matched by the part of the regular expression inside the parentheses. For e.g. ([0-9])+ will match 222 but not 837. If backreference are not required, you can optimize this regular expression Set(?:Value)? Backreferences can be used in expressions itself or in replacement text. For e.g. <([A-Za-z][A-Za-z0-9]*)>.*</> will match matching opening and closing tags. Grouping and Backreferences http://www.sherazsiddiqi.com/
  • 10. Unicode characters can be used as xxxx in regular expressions. For e.g. عطاری cat be matched in an expression as: 063906370627063106cc Unicode characters in Regular Exp. http://www.sherazsiddiqi.com/
  • 11. /i makes the regex match case insensitive. [A-Z] will match A and a with this modifier. /s enables "single-line mode". In this mode, the dot matches newlines as well. .* will match sherazattari with this modifier. /m enables "multi-line mode". In this mode, the caret and dollar match before and after newlines in the subject string. .* will match only sherazin sherazattari with this modifier. /x enables "free-spacing mode". In this mode, whitespace between regex tokens is ignored, and an unescaped # starts a comment. #sheraz.* will match only sherazin with this modifier. Regular Exp. Matching Modes http://www.sherazsiddiqi.com/
  • 12. A conditional is a special construct that will first evaluate a lookaround, and then execute one sub-regex if the lookaround succeeds, and another sub-regex if the lookaround fails. Example of Positive lookahead is: q(?=uv*)will match q in quvvvv and qu. Example of Negative lookahead is: q(?!uv*)will match q not followed by u and uv. Example of Positive lookbehind is: (?<=b)awill match a prefixed by b like ba. Example of Negative lookbehind is: (?<!b)awill match a not prefixed by b like ca and da etc. Lookarounds with Conditions… http://www.sherazsiddiqi.com/
  • 13. Example1:: I have an access log (access.log) file of Helix DNA server. I want to calculate how many times each content is access and update download and listen count of each content in the database. Exp: ^(.*)asxgen/Data/Naat/Download(.*)/(+)(mp3|rm)(.*)$ Replace: UPDATE DB.TBL set col=col + COUNT where id=; Example2:: I have application generated log (applog.txt) file of a web application. I want to fetch required information from relevant rows. In order to remove irrelevant rows: Exp: ^(?!((.*)ID:(.*)Status:(.*))).*$ Replace: Empty string Parse a log file… http://www.sherazsiddiqi.com/
  • 14. Questions Please….. http://www.sherazsiddiqi.com/ Thank you for being here… Most of the content is taken from: http://www.regular-expressions.info/ me@sherazsiddiqi.com