This document provides an introduction to regular expressions (regexes). It discusses that regexes are often feared by programmers due to their mysterious nature. It then defines what a regex is, as a small language for describing text that can be used to find and replace text. The document recommends learning regexes because they are heavily used in Ruby and are fast and encoding aware. It provides examples of basic regex usage in Ruby strings, including finding, replacing, and modifying strings. It also covers literal characters, character classes, shortcut character classes, and anchors in regexes.
Ciarán Walsh's PHPNW08 slides:
In the right hands regular expressions can be a powerful tool, but it’s also far too easy for them to be used badly, or in the wrong situations.
This talk will kick off with a look at alternatives to regular expressions, for when the power of pattern matching is not required, and will also go over some cases when there are better alternatives available.
Then there will be a brief refresher on pattern syntax and some general tips and tricks to help when constructing regular expressions, before we go on to look at some situations where the use of pattern matching is a good fit, how to solve some common problems, and some common pitfalls when writing patterns.
Ciarán Walsh's PHPNW08 slides:
In the right hands regular expressions can be a powerful tool, but it’s also far too easy for them to be used badly, or in the wrong situations.
This talk will kick off with a look at alternatives to regular expressions, for when the power of pattern matching is not required, and will also go over some cases when there are better alternatives available.
Then there will be a brief refresher on pattern syntax and some general tips and tricks to help when constructing regular expressions, before we go on to look at some situations where the use of pattern matching is a good fit, how to solve some common problems, and some common pitfalls when writing patterns.
Regular Expressions: every developer's best friend and worst nightmare! Join Andrei Zmievski, PHP developer and author of the PHP Regex (PCRE) extension, on a journey that will take you from your first steps into the world of regular expressions to the mastery of this most useful of tools. A must for everyone who's ever wondered what /(?=\d+)bar/ means.
^Regular Expressions is one of those tools that every developer should have in their toolbox. You can do your job without regular expressions, but knowing when and how to use them will make you a much more efficient and marketable developer. You'll learn how regular expressions can be used for validating user input, parsing text, and refactoring code. We'll also cover various tools that can be used to help you write and share expressions.$
Understanding regular expressions gives developers another extremely useful and powerful tool they can use to perform some operations that would otherwise be very tedious or difficult. This presentation goes over how to build and test regular expressions so developers can start using them within their own code.
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014Sandy Smith
Learn how to empower your objects in PHP with SPL! The SPL (Standard PHP Library) has existed for many years but is still one of the less understood parts of the language. We will cover some of the most useful core aspects of the SPL, features that are easy to add to your own objects, making them more powerful and flexible!
Lessons learned from years of maintaining/extending/improving an in house CMS. Hopefully these will present things to avoid and things to try that will save others some pain.
Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)Sandy Smith
You may know that queues can help with long-running tasks, but did you know they can help you make your application easier to debug, more performant, and scale in the cloud? Taking the real-world example of a contest app deployed on Azure, we'll see how easy queues can be to implement. You'll see how the smart use of queues can enable your application to handle many more users with the same code, break components across servers, and help you keep your app responsive.
Regular expressions are a concise way to search and transform strings using patterns. They are available in numerous text editors and programming languages.
Here, I introduce basic regular expressions and show the intermediate and advanced syntax which I use commonly. The second-to-last slide has a nice cheatsheet.
Related blog post: http://dafoster.net/articles/2013/09/04/regular-expressions/
Don't Fear the Regex - CapitalCamp/GovDays 2014Sandy Smith
Have you been scared off by Klingon-looking one-liners in Perl? Do you resort to writing complicated recursive functions just to parse some HTML? Don't!
I'll demystify regular expressions and show you how best to do them in PHP. We'll cover the syntax and functions that make PHP a great text-parsing language, and give you the foundation to learn more.
As a bonus, I'll give you two cases people often use as examples for regexes that PHP gives you better native ways to accomplish.
Given at CapitalCamp & GovDays 2014
Unicode regular expression tutorial with examples in Perl, PHP, and JavaScript.
Presented at: Shutterstock “Brown Bag Lunch” Tech Talk, 23 January 2013, New York, NY
Regular Expressions: every developer's best friend and worst nightmare! Join Andrei Zmievski, PHP developer and author of the PHP Regex (PCRE) extension, on a journey that will take you from your first steps into the world of regular expressions to the mastery of this most useful of tools. A must for everyone who's ever wondered what /(?=\d+)bar/ means.
^Regular Expressions is one of those tools that every developer should have in their toolbox. You can do your job without regular expressions, but knowing when and how to use them will make you a much more efficient and marketable developer. You'll learn how regular expressions can be used for validating user input, parsing text, and refactoring code. We'll also cover various tools that can be used to help you write and share expressions.$
Understanding regular expressions gives developers another extremely useful and powerful tool they can use to perform some operations that would otherwise be very tedious or difficult. This presentation goes over how to build and test regular expressions so developers can start using them within their own code.
Iterators, ArrayAccess & Countable (Oh My!) - Madison PHP 2014Sandy Smith
Learn how to empower your objects in PHP with SPL! The SPL (Standard PHP Library) has existed for many years but is still one of the less understood parts of the language. We will cover some of the most useful core aspects of the SPL, features that are easy to add to your own objects, making them more powerful and flexible!
Lessons learned from years of maintaining/extending/improving an in house CMS. Hopefully these will present things to avoid and things to try that will save others some pain.
Architecting with Queues for Scale, Speed, and Separation (DCPHP 3/11/15)Sandy Smith
You may know that queues can help with long-running tasks, but did you know they can help you make your application easier to debug, more performant, and scale in the cloud? Taking the real-world example of a contest app deployed on Azure, we'll see how easy queues can be to implement. You'll see how the smart use of queues can enable your application to handle many more users with the same code, break components across servers, and help you keep your app responsive.
Regular expressions are a concise way to search and transform strings using patterns. They are available in numerous text editors and programming languages.
Here, I introduce basic regular expressions and show the intermediate and advanced syntax which I use commonly. The second-to-last slide has a nice cheatsheet.
Related blog post: http://dafoster.net/articles/2013/09/04/regular-expressions/
Don't Fear the Regex - CapitalCamp/GovDays 2014Sandy Smith
Have you been scared off by Klingon-looking one-liners in Perl? Do you resort to writing complicated recursive functions just to parse some HTML? Don't!
I'll demystify regular expressions and show you how best to do them in PHP. We'll cover the syntax and functions that make PHP a great text-parsing language, and give you the foundation to learn more.
As a bonus, I'll give you two cases people often use as examples for regexes that PHP gives you better native ways to accomplish.
Given at CapitalCamp & GovDays 2014
Unicode regular expression tutorial with examples in Perl, PHP, and JavaScript.
Presented at: Shutterstock “Brown Bag Lunch” Tech Talk, 23 January 2013, New York, NY
Have you been scared off by Klingon-looking one-liners in Perl? Do you resort to writing complicated recursive functions just to parse some HTML? Don't!
I'll demystify regular expressions and show you how best to do them in PHP. We'll cover the syntax and functions that make PHP a great text-parsing language, and give you the foundation to learn more.
As a bonus, I'll give you two cases people often use as examples for regexes that PHP gives you better native ways to accomplish.
GAIQ - Regular expressions-google-analytics; one of the best resources; concepts explained in a very easy to understand descriptive method with examples
Architecting with Queues - Northeast PHP 2015Sandy Smith
You may know that queues can help with long-running tasks, but did you know they can help you make your application easier to debug, more performant, and scale in the cloud? Taking the real-world example of a contest app, we’ll see how easy queues can be to implement. You’ll see how the smart use of queues can enable your application to handle many more users with the same code, break components across servers, and help you keep your app responsive.
This is the ninth set of slightly updated slides from a Perl programming course that I held some years ago.
I want to share it with everyone looking for intransitive Perl-knowledge.
A table of content for all presentations can be found at i-can.eu.
The source code for the examples and the presentations in ODP format are on https://github.com/kberov/PerlProgrammingCourse
And now you have two problems. Ruby regular expressions for fun and profit by...Codemotion
A wise hacker said: Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.
Regular expressions are a powerful tool in our hands and a first class citizen in ruby so it is tempting to overuse them. But knowing them and using them properly is a fundamental asset of every developer.
We’ll see hands-on examples of proper Reg Exps usage in ruby code, we’ll also look at bad and ugly cases and learn how to approach writing, testing and debugging regular expressions.
Using Regular Expressions and Staying SaneCarl Brown
Presentation I gave to the local http://www.cocoacoder.org/ meeting on using Regular Expression in Cocoa code (although much of it applies to other languages as well).
GridMate - End to end testing is a critical piece to ensure quality and avoid...ThomasParaiso2
End to end testing is a critical piece to ensure quality and avoid regressions. In this session, we share our journey building an E2E testing pipeline for GridMate components (LWC and Aura) using Cypress, JSForce, FakerJS…
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...Neo4j
Leonard Jayamohan, Partner & Generative AI Lead, Deloitte
This keynote will reveal how Deloitte leverages Neo4j’s graph power for groundbreaking digital twin solutions, achieving a staggering 100x performance boost. Discover the essential role knowledge graphs play in successful generative AI implementations. Plus, get an exclusive look at an innovative Neo4j + Generative AI solution Deloitte is developing in-house.
Essentials of Automations: The Art of Triggers and Actions in FMESafe Software
In this second installment of our Essentials of Automations webinar series, we’ll explore the landscape of triggers and actions, guiding you through the nuances of authoring and adapting workspaces for seamless automations. Gain an understanding of the full spectrum of triggers and actions available in FME, empowering you to enhance your workspaces for efficient automation.
We’ll kick things off by showcasing the most commonly used event-based triggers, introducing you to various automation workflows like manual triggers, schedules, directory watchers, and more. Plus, see how these elements play out in real scenarios.
Whether you’re tweaking your current setup or building from the ground up, this session will arm you with the tools and insights needed to transform your FME usage into a powerhouse of productivity. Join us to discover effective strategies that simplify complex processes, enhancing your productivity and transforming your data management practices with FME. Let’s turn complexity into clarity and make your workspaces work wonders!
Removing Uninteresting Bytes in Software FuzzingAftab Hussain
Imagine a world where software fuzzing, the process of mutating bytes in test seeds to uncover hidden and erroneous program behaviors, becomes faster and more effective. A lot depends on the initial seeds, which can significantly dictate the trajectory of a fuzzing campaign, particularly in terms of how long it takes to uncover interesting behaviour in your code. We introduce DIAR, a technique designed to speedup fuzzing campaigns by pinpointing and eliminating those uninteresting bytes in the seeds. Picture this: instead of wasting valuable resources on meaningless mutations in large, bloated seeds, DIAR removes the unnecessary bytes, streamlining the entire process.
In this work, we equipped AFL, a popular fuzzer, with DIAR and examined two critical Linux libraries -- Libxml's xmllint, a tool for parsing xml documents, and Binutil's readelf, an essential debugging and security analysis command-line tool used to display detailed information about ELF (Executable and Linkable Format). Our preliminary results show that AFL+DIAR does not only discover new paths more quickly but also achieves higher coverage overall. This work thus showcases how starting with lean and optimized seeds can lead to faster, more comprehensive fuzzing campaigns -- and DIAR helps you find such seeds.
- These are slides of the talk given at IEEE International Conference on Software Testing Verification and Validation Workshop, ICSTW 2022.
Communications Mining Series - Zero to Hero - Session 1DianaGray10
This session provides introduction to UiPath Communication Mining, importance and platform overview. You will acquire a good understand of the phases in Communication Mining as we go over the platform with you. Topics covered:
• Communication Mining Overview
• Why is it important?
• How can it help today’s business and the benefits
• Phases in Communication Mining
• Demo on Platform overview
• Q/A
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024Neo4j
Neha Bajwa, Vice President of Product Marketing, Neo4j
Join us as we explore breakthrough innovations enabled by interconnected data and AI. Discover firsthand how organizations use relationships in data to uncover contextual insights and solve our most pressing challenges – from optimizing supply chains, detecting fraud, and improving customer experiences to accelerating drug discoveries.
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...SOFTTECHHUB
The choice of an operating system plays a pivotal role in shaping our computing experience. For decades, Microsoft's Windows has dominated the market, offering a familiar and widely adopted platform for personal and professional use. However, as technological advancements continue to push the boundaries of innovation, alternative operating systems have emerged, challenging the status quo and offering users a fresh perspective on computing.
One such alternative that has garnered significant attention and acclaim is Nitrux Linux 3.5.0, a sleek, powerful, and user-friendly Linux distribution that promises to redefine the way we interact with our devices. With its focus on performance, security, and customization, Nitrux Linux presents a compelling case for those seeking to break free from the constraints of proprietary software and embrace the freedom and flexibility of open-source computing.
Securing your Kubernetes cluster_ a step-by-step guide to success !KatiaHIMEUR1
Today, after several years of existence, an extremely active community and an ultra-dynamic ecosystem, Kubernetes has established itself as the de facto standard in container orchestration. Thanks to a wide range of managed services, it has never been so easy to set up a ready-to-use Kubernetes cluster.
However, this ease of use means that the subject of security in Kubernetes is often left for later, or even neglected. This exposes companies to significant risks.
In this talk, I'll show you step-by-step how to secure your Kubernetes cluster for greater peace of mind and reliability.
A tale of scale & speed: How the US Navy is enabling software delivery from l...sonjaschweigert1
Rapid and secure feature delivery is a goal across every application team and every branch of the DoD. The Navy’s DevSecOps platform, Party Barge, has achieved:
- Reduction in onboarding time from 5 weeks to 1 day
- Improved developer experience and productivity through actionable findings and reduction of false positives
- Maintenance of superior security standards and inherent policy enforcement with Authorization to Operate (ATO)
Development teams can ship efficiently and ensure applications are cyber ready for Navy Authorizing Officials (AOs). In this webinar, Sigma Defense and Anchore will give attendees a look behind the scenes and demo secure pipeline automation and security artifacts that speed up application ATO and time to production.
We will cover:
- How to remove silos in DevSecOps
- How to build efficient development pipeline roles and component templates
- How to deliver security artifacts that matter for ATO’s (SBOMs, vulnerability reports, and policy evidence)
- How to streamline operations with automated policy checks on container images
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfPaige Cruz
Monitoring and observability aren’t traditionally found in software curriculums and many of us cobble this knowledge together from whatever vendor or ecosystem we were first introduced to and whatever is a part of your current company’s observability stack.
While the dev and ops silo continues to crumble….many organizations still relegate monitoring & observability as the purview of ops, infra and SRE teams. This is a mistake - achieving a highly observable system requires collaboration up and down the stack.
I, a former op, would like to extend an invitation to all application developers to join the observability party will share these foundational concepts to build on:
Alt. GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using ...James Anderson
Effective Application Security in Software Delivery lifecycle using Deployment Firewall and DBOM
The modern software delivery process (or the CI/CD process) includes many tools, distributed teams, open-source code, and cloud platforms. Constant focus on speed to release software to market, along with the traditional slow and manual security checks has caused gaps in continuous security as an important piece in the software supply chain. Today organizations feel more susceptible to external and internal cyber threats due to the vast attack surface in their applications supply chain and the lack of end-to-end governance and risk management.
The software team must secure its software delivery process to avoid vulnerability and security breaches. This needs to be achieved with existing tool chains and without extensive rework of the delivery processes. This talk will present strategies and techniques for providing visibility into the true risk of the existing vulnerabilities, preventing the introduction of security issues in the software, resolving vulnerabilities in production environments quickly, and capturing the deployment bill of materials (DBOM).
Speakers:
Bob Boule
Robert Boule is a technology enthusiast with PASSION for technology and making things work along with a knack for helping others understand how things work. He comes with around 20 years of solution engineering experience in application security, software continuous delivery, and SaaS platforms. He is known for his dynamic presentations in CI/CD and application security integrated in software delivery lifecycle.
Gopinath Rebala
Gopinath Rebala is the CTO of OpsMx, where he has overall responsibility for the machine learning and data processing architectures for Secure Software Delivery. Gopi also has a strong connection with our customers, leading design and architecture for strategic implementations. Gopi is a frequent speaker and well-known leader in continuous delivery and integrating security into software delivery.
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Albert Hoitingh
In this session I delve into the encryption technology used in Microsoft 365 and Microsoft Purview. Including the concepts of Customer Key and Double Key Encryption.
Threats to mobile devices are more prevalent and increasing in scope and complexity. Users of mobile devices desire to take full advantage of the features
available on those devices, but many of the features provide convenience and capability but sacrifice security. This best practices guide outlines steps the users can take to better protect personal devices and information.
Unlocking Productivity: Leveraging the Potential of Copilot in Microsoft 365, a presentation by Christoforos Vlachos, Senior Solutions Manager – Modern Workplace, Uni Systems
Transcript: Selling digital books in 2024: Insights from industry leaders - T...BookNet Canada
The publishing industry has been selling digital audiobooks and ebooks for over a decade and has found its groove. What’s changed? What has stayed the same? Where do we go from here? Join a group of leading sales peers from across the industry for a conversation about the lessons learned since the popularization of digital books, best practices, digital book supply chain management, and more.
Link to video recording: https://bnctechforum.ca/sessions/selling-digital-books-in-2024-insights-from-industry-leaders/
Presented by BookNet Canada on May 28, 2024, with support from the Department of Canadian Heritage.
4. The Fear Factor!
For unknown reasons regular expressions
are deeply shrouded in mystery
5. The Fear Factor!
For unknown reasons regular expressions
are deeply shrouded in mystery
Many programmers outright fear them
6. The Fear Factor!
For unknown reasons regular expressions
are deeply shrouded in mystery
Many programmers outright fear them
I stumped a room full of programmers
in Tulsa by shouting out a two
character expression
7. The Fear Factor!
For unknown reasons regular expressions
are deeply shrouded in mystery
Many programmers outright fear them
I stumped a room full of programmers
in Tulsa by shouting out a two
character expression
I have know idea why this is
9. What is a Regex?
Regular expression is a very small
language for describing text
10. What is a Regex?
Regular expression is a very small
language for describing text
You can use them to dissect and change
textual data
11. What is a Regex?
Regular expression is a very small
language for describing text
You can use them to dissect and change
textual data
I think of them as a DSL for find and
replace operations
14. Why Learn Regular Expressions?
Ruby leans heavily on regular expressions:
Many text operations in Ruby are
easiest with the right regex
15. Why Learn Regular Expressions?
Ruby leans heavily on regular expressions:
Many text operations in Ruby are
easiest with the right regex
Regular expressions are fast
16. Why Learn Regular Expressions?
Ruby leans heavily on regular expressions:
Many text operations in Ruby are
easiest with the right regex
Regular expressions are fast
Regular expressions are encoding aware
17. Why Learn Regular Expressions?
Ruby leans heavily on regular expressions:
Many text operations in Ruby are
easiest with the right regex
Regular expressions are fast
Regular expressions are encoding aware
You can be the one scaring all the other
programmers
22. Basic Regex Usage
Strings has methods
supporting:
Find/Find All
Replace/Replace All
Use sub!()/gsub!() to
modify a String in
place
23. Basic Regex Usage
Strings has methods if "100" =~ /Ad+z/
supporting: puts "This is a number."
end
Find/Find All
Replace/Replace All
Use sub!()/gsub!() to
modify a String in
place
24. Basic Regex Usage
Strings has methods if "100" =~ /Ad+z/
supporting: puts "This is a number."
end
"Find all, words.".scan(/w+/) do |word|
Find/Find All puts word.downcase
end
year, month, day = "2008-09-04".scan(/d+/)
Replace/Replace All
Use sub!()/gsub!() to
modify a String in
place
25. Basic Regex Usage
Strings has methods if "100" =~ /Ad+z/
supporting: puts "This is a number."
end
"Find all, words.".scan(/w+/) do |word|
Find/Find All puts word.downcase
end
year, month, day = "2008-09-04".scan(/d+/)
Replace/Replace All
csv = "C, S, V".sub(/,s+/, ",")
cap = "one two".sub(/w+/) { |n| n.capitalize }
Use sub!()/gsub!() to
modify a String in
place
26. Basic Regex Usage
Strings has methods if "100" =~ /Ad+z/
supporting: puts "This is a number."
end
"Find all, words.".scan(/w+/) do |word|
Find/Find All puts word.downcase
end
year, month, day = "2008-09-04".scan(/d+/)
Replace/Replace All
csv = "C, S, V".sub(/,s+/, ",")
cap = "one two".sub(/w+/) { |n| n.capitalize }
Use sub!()/gsub!() to
modify a String in csv = "C, S, V".gsub(/,s+/, ",")
caps = "one two".gsub(/w+/) { |n| n.capitalize }
place
30. Literal Characters
Most characters in a regex match
themselves literally
The only special characters are:
[].^$?*+{}|()
You can proceed a special character
with to make it literal
31. Literal Characters
Most characters in a regex match
themselves literally
The only special characters are:
[].^$?*+{}|()
You can proceed a special character
with to make it literal
The regex /James Gray/ matches my name
34. Character Classes
Characters in [ … ] are choices for a
single character match
A leading ^ negates the class, so [^ … ]
matches what is not listed
35. Character Classes
Characters in [ … ] are choices for a
single character match
A leading ^ negates the class, so [^ … ]
matches what is not listed
You can use ranges like a-z or 0-9
36. Character Classes
Characters in [ … ] are choices for a
single character match
A leading ^ negates the class, so [^ … ]
matches what is not listed
You can use ranges like a-z or 0-9
The expression /[bcr]at/ will match
“bat,” “cat,” or “rat”
50. Anchors
Anchor Matches
Anchors match
between characters A Start of the String
End of the String or
Z
They are used to assert before trailing newline
that the content you z End of the String
want must appear in a ^ Start of a line
certain place
$ End of a line
Thus /^Totals/ searches Between wW or Ww,
b
for a line starting with and at A and z
“Totals” B Between ww or WW
51. Anchors
Anchor Matches
Anchors match
between characters A Start of the String
End of the String or
Z
They are used to assert before trailing newline
that the content you z End of the String
want must appear in a ^ Start of a line
certain place
$ End of a line
Thus /^Totals/ searches Between wW or Ww,
b
for a line starting with and at A and z
“Totals” B Between ww or WW
52. Anchors
Anchor Matches
Anchors match
between characters A Start of the String
End of the String or
Z
They are used to assert before trailing newline
that the content you z End of the String
want must appear in a ^ Start of a line
certain place
$ End of a line
Thus /^Totals/ searches Between wW or Ww,
b
for a line starting with and at A and z
“Totals” B Between ww or WW
53. Anchors
Anchor Matches
Anchors match
between characters A Start of the String
End of the String or
Z
They are used to assert before trailing newline
that the content you z End of the String
want must appear in a ^ Start of a line
certain place
$ End of a line
Thus /^Totals/ searches Between wW or Ww,
b
for a line starting with and at A and z
“Totals” B Between ww or WW
54. Anchors
Anchor Matches
Anchors match
between characters A Start of the String
End of the String or
Z
They are used to assert before trailing newline
that the content you z End of the String
want must appear in a ^ Start of a line
certain place
$ End of a line
Thus /^Totals/ searches Between wW or Ww,
b
for a line starting with and at A and z
“Totals” B Between ww or WW
55. Anchors
Anchor Matches
Anchors match
between characters A Start of the String
End of the String or
Z
They are used to assert before trailing newline
that the content you z End of the String
want must appear in a ^ Start of a line
certain place
$ End of a line
Thus /^Totals/ searches Between wW or Ww,
b
for a line starting with and at A and z
“Totals” B Between ww or WW
56. Anchors
Anchor Matches
Anchors match
between characters A Start of the String
End of the String or
Z
They are used to assert before trailing newline
that the content you z End of the String
want must appear in a ^ Start of a line
certain place
$ End of a line
Thus /^Totals/ searches Between wW or Ww,
b
for a line starting with and at A and z
“Totals” B Between ww or WW
57. Anchors
Anchor Matches
Anchors match
between characters A Start of the String
End of the String or
Z
They are used to assert before trailing newline
that the content you z End of the String
want must appear in a ^ Start of a line
certain place
$ End of a line
Thus /^Totals/ searches Between wW or Ww,
b
for a line starting with and at A and z
“Totals” B Between ww or WW
59. Repetition
You can tack symbols
onto an element of a
regex to indicate that
element can repeat
60. Repetition
You can tack symbols
onto an element of a
regex to indicate that
element can repeat
The expression /ab+c?/
matches an a, followed
by one or more b’s, and
optionally followed by
ac
61. Repetition
You can tack symbols Repeater Allowed Count
onto an element of a
? Zero or one
regex to indicate that
element can repeat + One or more
* Zero or more
The expression /ab+c?/ {n} Exactly n
matches an a, followed {n,} At least n
by one or more b’s, and
{,m} No more than m
optionally followed by
ac {n,m} Between n and m
62. Repetition
You can tack symbols Repeater Allowed Count
onto an element of a
? Zero or one
regex to indicate that
element can repeat + One or more
* Zero or more
The expression /ab+c?/ {n} Exactly n
matches an a, followed {n,} At least n
by one or more b’s, and
{,m} No more than m
optionally followed by
ac {n,m} Between n and m
63. Repetition
You can tack symbols Repeater Allowed Count
onto an element of a
? Zero or one
regex to indicate that
element can repeat + One or more
* Zero or more
The expression /ab+c?/ {n} Exactly n
matches an a, followed {n,} At least n
by one or more b’s, and
{,m} No more than m
optionally followed by
ac {n,m} Between n and m
64. Repetition
You can tack symbols Repeater Allowed Count
onto an element of a
? Zero or one
regex to indicate that
element can repeat + One or more
* Zero or more
The expression /ab+c?/ {n} Exactly n
matches an a, followed {n,} At least n
by one or more b’s, and
{,m} No more than m
optionally followed by
ac {n,m} Between n and m
65. Repetition
You can tack symbols Repeater Allowed Count
onto an element of a
? Zero or one
regex to indicate that
element can repeat + One or more
* Zero or more
The expression /ab+c?/ {n} Exactly n
matches an a, followed {n,} At least n
by one or more b’s, and
{,m} No more than m
optionally followed by
ac {n,m} Between n and m
66. Repetition
You can tack symbols Repeater Allowed Count
onto an element of a
? Zero or one
regex to indicate that
element can repeat + One or more
* Zero or more
The expression /ab+c?/ {n} Exactly n
matches an a, followed {n,} At least n
by one or more b’s, and
{,m} No more than m
optionally followed by
ac {n,m} Between n and m
67. Repetition
You can tack symbols Repeater Allowed Count
onto an element of a
? Zero or one
regex to indicate that
element can repeat + One or more
* Zero or more
The expression /ab+c?/ {n} Exactly n
matches an a, followed {n,} At least n
by one or more b’s, and
{,m} No more than m
optionally followed by
ac {n,m} Between n and m
68. Repetition
You can tack symbols Repeater Allowed Count
onto an element of a
? Zero or one
regex to indicate that
element can repeat + One or more
* Zero or more
The expression /ab+c?/ {n} Exactly n
matches an a, followed {n,} At least n
by one or more b’s, and
{,m} No more than m
optionally followed by
ac {n,m} Between n and m
70. Some Examples
if var =~ /As*z/
puts "Variable is blank."
end
71. Some Examples
if var =~ /As*z/
puts "Variable is blank."
end
if var !~ /S/
puts "Variable is blank."
end
72. Some Examples
if var =~ /As*z/
puts "Variable is blank."
end
if var !~ /S/
puts "Variable is blank."
end
From TopCoder.com, SRM 216 “CultureShock:”
Bob and Doug have recently moved from Canada to the United States, and they are confused
by this strange letter, "ZEE". They need your assistance. Given a String text, replace every
occurrence of the word, "ZEE", with the word, "ZED", and return the result.
Note that if "ZEE" is just part of a larger word (for example, "ZEES"), it should not be altered.
73. Some Examples
if var =~ /As*z/
puts "Variable is blank."
end
if var !~ /S/
puts "Variable is blank."
end
From TopCoder.com, SRM 216 “CultureShock:”
Bob and Doug have recently moved from Canada to the United States, and they are confused
by this strange letter, "ZEE". They need your assistance. Given a String text, replace every
occurrence of the word, "ZEE", with the word, "ZED", and return the result.
Note that if "ZEE" is just part of a larger word (for example, "ZEES"), it should not be altered.
solution = text.gsub(/bZEEb/, "ZED")
75. Greedy Verses Non-Greedy
By default repetition
will always be greedy,
consuming as many
characters as possible
76. Greedy Verses Non-Greedy
By default repetition
will always be greedy,
consuming as many
characters as possible
The match will
backtrack, giving
up characters, if it
helps it succeed
77. Greedy Verses Non-Greedy
By default repetition
will always be greedy,
consuming as many
characters as possible
The match will
backtrack, giving
up characters, if it
helps it succeed
You can negate this,
matching minimal
characters
78. Greedy Verses Non-Greedy
By default repetition
will always be greedy,
consuming as many Greedy Non-Greedy
characters as possible ? ??
+ +?
The match will
* *?
backtrack, giving
up characters, if it {n} N/A
helps it succeed {n,} {n,}?
{,m} {,m}?
You can negate this, {n,m} {n,m}?
matching minimal
characters
79. Greedy Verses Non-Greedy
By default repetition
will always be greedy,
consuming as many Greedy Non-Greedy
characters as possible ? ??
+ +?
The match will
* *?
backtrack, giving
up characters, if it {n} N/A
helps it succeed {n,} {n,}?
{,m} {,m}?
You can negate this, {n,m} {n,m}?
matching minimal
characters
80. Greedy Verses Non-Greedy
By default repetition
will always be greedy,
consuming as many Greedy Non-Greedy
characters as possible ? ??
+ +?
The match will
* *?
backtrack, giving
up characters, if it {n} N/A
helps it succeed {n,} {n,}?
{,m} {,m}?
You can negate this, {n,m} {n,m}?
matching minimal
characters
81. Greedy Verses Non-Greedy
By default repetition
will always be greedy,
consuming as many Greedy Non-Greedy
characters as possible ? ??
+ +?
The match will
* *?
backtrack, giving
up characters, if it {n} N/A
helps it succeed {n,} {n,}?
{,m} {,m}?
You can negate this, {n,m} {n,m}?
matching minimal
characters
82. Greedy Verses Non-Greedy
By default repetition
will always be greedy,
consuming as many Greedy Non-Greedy
characters as possible ? ??
+ +?
The match will
* *?
backtrack, giving
up characters, if it {n} N/A
helps it succeed {n,} {n,}?
{,m} {,m}?
You can negate this, {n,m} {n,m}?
matching minimal
characters
83. Greedy Verses Non-Greedy
By default repetition
will always be greedy,
consuming as many Greedy Non-Greedy
characters as possible ? ??
+ +?
The match will
* *?
backtrack, giving
up characters, if it {n} N/A
helps it succeed {n,} {n,}?
{,m} {,m}?
You can negate this, {n,m} {n,m}?
matching minimal
characters
84. Greedy Verses Non-Greedy
By default repetition
will always be greedy,
consuming as many Greedy Non-Greedy
characters as possible ? ??
+ +?
The match will
* *?
backtrack, giving
up characters, if it {n} N/A
helps it succeed {n,} {n,}?
{,m} {,m}?
You can negate this, {n,m} {n,m}?
matching minimal
characters
85. Greedy Verses Non-Greedy
By default repetition
will always be greedy,
consuming as many Greedy Non-Greedy
characters as possible ? ??
+ +?
The match will
* *?
backtrack, giving
up characters, if it {n} N/A
helps it succeed {n,} {n,}?
{,m} {,m}?
You can negate this, {n,m} {n,m}?
matching minimal
characters
88. Alternation
In a regex, | means “or”
You can put a full expression on the left
and another full expression on the right
89. Alternation
In a regex, | means “or”
You can put a full expression on the left
and another full expression on the right
Either can match
90. Alternation
In a regex, | means “or”
You can put a full expression on the left
and another full expression on the right
Either can match
The expression /James|words?/ will
match “James,” “word,” or “words”
92. Grouping
Everything in ( … ) is grouped into a
single element for the purposes of
repetition and alternation
93. Grouping
Everything in ( … ) is grouped into a
single element for the purposes of
repetition and alternation
The expression /(ha)+/ matches “ha,”
“haha,” “hahaha,” etc.
94. Grouping
Everything in ( … ) is grouped into a
single element for the purposes of
repetition and alternation
The expression /(ha)+/ matches “ha,”
“haha,” “hahaha,” etc.
The expression /Greg(ory)?/ matches
“Greg” and “Gregory”
97. Captures
( … ) also capture
what they match
After a match, you can
access these captures
in the variables $1, $2,
etc., from left to right
98. Captures
( … ) also capture
what they match
After a match, you can
access these captures
in the variables $1, $2,
etc., from left to right
Use 1, 2, etc. in
String replacements
99. Captures
( … ) also capture
what they match
"$99.95" =~ /$(d+(.d+)?)/
After a match, you can
access these captures
in the variables $1, $2,
etc., from left to right
Use 1, 2, etc. in
String replacements
100. Captures
( … ) also capture
what they match
"$99.95" =~ /$(d+(.d+)?)/
After a match, you can
access these captures
in the variables $1, $2,
$1
etc., from left to right
Use 1, 2, etc. in
String replacements
101. Captures
( … ) also capture
what they match
"$99.95" =~ /$(d+(.d+)?)/
After a match, you can
access these captures
in the variables $1, $2,
$1
etc., from left to right $2
Use 1, 2, etc. in
String replacements
105. Modes
Regular expressions have modes
End an expression with /i to make the
expression case insensitive
End with /m for “multi-line” mode
where . will also match newlines
106. Modes
Regular expressions have modes
End an expression with /i to make the
expression case insensitive
End with /m for “multi-line” mode
where . will also match newlines
Use /x to add space and comments
107. Modes
Regular expressions have modes
End an expression with /i to make the
expression case insensitive
End with /m for “multi-line” mode
where . will also match newlines
Use /x to add space and comments
You can combine modes: /mi
109. More Examples
if ip =~ /Ad{1,3}(.d{1,3}){3}z/
puts "IP adress is well formed."
end
110. More Examples
if ip =~ /Ad{1,3}(.d{1,3}){3}z/
puts "IP adress is well formed."
end
if text =~ /b(at|for|in)[.?!]/
puts "You have bad grammar."
end
111. More Examples
if ip =~ /Ad{1,3}(.d{1,3}){3}z/
puts "IP adress is well formed."
end
if text =~ /b(at|for|in)[.?!]/
puts "You have bad grammar."
end
james_gray = "Gray, James".sub(/(S+),s*(.+)/, '2 1')
114. Other Tricks
There are other special
variables for regexen
including $`, $&, and $’
You can escape content
for use in a regex
115. Other Tricks
There are other special
variables for regexen
including $`, $&, and $’
You can escape content
for use in a regex
There’s a MatchData
object for matches
116. Other Tricks
There are other special
variables for regexen
including $`, $&, and $’
You can escape content
for use in a regex
There’s a MatchData
object for matches
Many methods can take
a regex
117. Other Tricks
"one_two_three" =~ /two/
There are other special one_, two, _three = $`, $&, $'
variables for regexen
including $`, $&, and $’
You can escape content
for use in a regex
There’s a MatchData
object for matches
Many methods can take
a regex
118. Other Tricks
"one_two_three" =~ /two/
There are other special one_, two, _three = $`, $&, $'
variables for regexen
print "What's your favorite language? "
including $`, $&, and $’ lang = $stdin.gets.strip
if "Perl Java" =~ /b#{Regexp.escape(lang)}b/i
puts "You are weird."
else
You can escape content puts "OK."
end
for use in a regex
There’s a MatchData
object for matches
Many methods can take
a regex
119. Other Tricks
"one_two_three" =~ /two/
There are other special one_, two, _three = $`, $&, $'
variables for regexen
print "What's your favorite language? "
including $`, $&, and $’ lang = $stdin.gets.strip
if "Perl Java" =~ /b#{Regexp.escape(lang)}b/i
puts "You are weird."
else
You can escape content puts "OK."
end
for use in a regex
CONFIG_RE = /A([^=s]+)s*=s*(S+)/
config = "url = http://ruby-lang.org"
There’s a MatchData key, value = config.match(CONFIG_RE).captures
object for matches
Many methods can take
a regex
120. Other Tricks
"one_two_three" =~ /two/
There are other special one_, two, _three = $`, $&, $'
variables for regexen
print "What's your favorite language? "
including $`, $&, and $’ lang = $stdin.gets.strip
if "Perl Java" =~ /b#{Regexp.escape(lang)}b/i
puts "You are weird."
else
You can escape content puts "OK."
end
for use in a regex
CONFIG_RE = /A([^=s]+)s*=s*(S+)/
config = "url = http://ruby-lang.org"
There’s a MatchData key, value = config.match(CONFIG_RE).captures
object for matches
fields = "1|2 | 3".split(/s*|s*/)
last_word_i = "one two three".rindex(/bw+/)
Many methods can take
five = "Count: 5"[/d+/]
a regex five = "Count: 5"[/Count:s*(d+)/, 1]
134. Regular Expression Extensions
Ruby’s regex engine
adds several common
extensions
These usually look
something like
(? … )
135. Regular Expression Extensions
Ruby’s regex engine
adds several common
extensions
These usually look
something like
(? … )
The simplest is (?: … )
which is grouping
without capturing
136. Regular Expression Extensions
Ruby’s regex engine
adds several common
data = "put the ball in the sack"
extensions re = %r{
(put|set) # verb: $1
s+ # some space (/x safe)
(?:(?:the|a)s+)? # an article (optional)
These usually look (w+) # noun: $2
s+
something like (?:in(?:side)?)? # preposition (optional)
s+
(? … ) (?:(?:the|a)s+)?
(w+) # noun: $3
}x
p data =~ re
The simplest is (?: … ) p [$1, $2, $3]
which is grouping
without capturing
139. Look-Around Assertions
You can use look-ahead
assertions to peek
ahead without
consuming characters:
(?= … ) and (?! … )
140. Look-Around Assertions
You can use look-ahead
assertions to peek
ahead without
consuming characters:
(?= … ) and (?! … )
Ruby 1.9 adds a fixed
look-behind:
141. Look-Around Assertions
You can use look-ahead
assertions to peek
ahead without
consuming characters:
(?= … ) and (?! … )
Ruby 1.9 adds a fixed
look-behind:
(?<= … ) and
(?<! … )
142. Look-Around Assertions
You can use look-ahead
assertions to peek
ahead without
consuming characters:
class Numeric
def commify
(?= … ) and (?! … ) to_s.reverse.
gsub(/(ddd)(?=d)(?!d*.)/, '1,').
reverse
end
Ruby 1.9 adds a fixed end
look-behind:
(?<= … ) and
(?<! … )
147. Oniguruma
Ruby 1.9’s regex engine
is faster and more
powerful:
Named groups
Nested matching
Improved encodings
148. Oniguruma
Ruby 1.9’s regex engine
is faster and more
powerful:
Named groups
Nested matching
Improved encodings
And more…
149. Oniguruma
Ruby 1.9’s regex engine
is faster and more config = "mode = wrap"
if /A(?<key>w+)s*=s*(?<value>w+)/ =~ config
powerful: puts "Key is #{key} and value is #{value}"
end
Named groups
Nested matching
Improved encodings
And more…
150. Oniguruma
Ruby 1.9’s regex engine
is faster and more config = "mode = wrap"
if /A(?<key>w+)s*=s*(?<value>w+)/ =~ config
powerful: puts "Key is #{key} and value is #{value}"
end
Named groups
CHECK = /A(?<paren>((g<paren>|[^()])*?))z/
%w[ ()
(()())
Nested matching (a(b(c,d())))
()) ].each do |test|
unless test =~ CHECK
puts "#{test} isn't balanced"
Improved encodings end
end
And more…
153. The Data
data = <<END_FIELDS.gsub(/s+/, " ")
Business Name (Text Field),
Allows Pets (Check),
Open To (Dropdown: Men, Women, Children, Any),
Atmosphere (Check List: Calm, Romantic, New Age)
END_FIELDS