GithubHelp home page GithubHelp logo

billyott / regex-practice-ruby-methods-with-regular-expressions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from learn-co-curriculum/regex-practice-ruby-methods-with-regular-expressions

0.0 0.0 0.0 6 KB

License: Other

regex-practice-ruby-methods-with-regular-expressions's Introduction

Ruby Methods with Regex

Objectives

  • Use the match method
  • Use capture groups
  • Use the scan method
  • Understand the diff between match and scan

Ruby Methods with Regex

You've learned the basics of writing Regular Expressions in Ruby. In this lesson we'll learn how to use regular expressions with the match and scan ruby methods. We'll also learn to use capture groups.

Scan

The scan method returns an array of all items in your string that match a given Regular Expression. For example:

"The rain in Spain lies mainly in the plain".scan(/\w+ain/)
=> ["rain", "Spain", "main", "plain"]

Match

The match method returns the first item in your string that matches a given Regular Expression as a MatchData object. For example:

"The rain in Spain lies mainly in the plain".match(/\w+ain/)
=> #<MatchData "rain"> 

"The rain in Spain lies mainly in the plain".match(/France/)
=> nil

More often than not, we use the result of the match method as a boolean, indicating the existence of the pattern in the given string.

Grep

Grep is an enumerable method for pattern searching in arrays and hashes. Similar to scan, grep will return an array of matching items from an array.

names = ["Jeri Faria", "Althea Voth", "Audry Donoho", "Scotty Chaves", "Lance Barrio", "Zachary Newhall", "Stefany Janey", "Tressie Kinsel", "Raven Grimsley", "Marketta Gaylor", "Leota Crowe", "Mazie Norman", "Damien Loffredo"]

#Get items from array where first name has five letters:
names.grep(/^\w{5}\s/)

=> ["Audry Donoho", "Lance Barrio", "Raven Grimsley", "Leota Crowe", "Mazie Norman"]

Capture Groups

Using parentheses in our regex allows us to create 'groups' that we can refer to in our scan/match/grep methods as indexes in an array. In the example below we create three capture groups for the three sets of digits in a phone number. Now, when we scan a list of numbers, each phone number is broken down into subgroups based on the capture groups we built in our regular expressions:

numbers = "202-555-0192 202-555-0147 202-555-0131 202-555-0116 202-555-0192 202-555-0197"

number_breakdown = numbers.scan(/(\d+)-(\d+)-(\d+)/)
=> [["202", "555", "0192"], ["202", "555", "0147"], ["202", "555", "0131"], ["202", "555", "0116"], ["202", "555", "0192"], ["202", "555", "0197"]] 

number_breakdown[0]
=> ["202", "555", "0192"]

number_breakdown[0][1]
=> "555"

regex-practice-ruby-methods-with-regular-expressions's People

Contributors

annjohn avatar dfenjves avatar pletcher avatar lawrend avatar franknowinski avatar howardbdev avatar onyoo avatar maxwellbenton avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.