Coder Social home page Coder Social logo

regular-expression-knowledge-we-have-discovered's Introduction

Knowledge we have discovered about regular expressions:

Methods

  • =~ Returns the index of the first match

  • [] return the first match

  • gsub Takes a regex expression, and a second argument. Replaces all instances of the target (first argument) with the second argument.

    a = "Hello, its me!"
    
    a.gsub(/e/, "pizza") #=> "Hpizzallo, it's mpizza!"
    a.gsub(/l/, "pizza") #=> "Hepizzapizzao, it's me!"
  • split Splits string based on the character argumert provided (a.split(/<character(s)>/). Removes the arg character(s) from the string, returns an array of strings

    a = "Hello, its me!"
    a.split           # =>  ["Hello,", "its", "me!"]
    a.split(/i/)      # =>  ["Hello, ", "ts me!"]
    a.split(/ll/)     # =>  ["He", "o, its me!"]
    a.split(/z/)      # =>  ["Hello, its me!"]
  • scan Returns an array of matches

Things that can be in a regular expression

  • /P/ A character, anywhere in the string

  • /PM/ Two characters next to each other

  • /M+/ One or more of its target

  • /M*/ Zero or more of its target

  • /M?/ Zero or one of its target

    "b ba baa baaa baaaaa".scan(/ba?/) # => ["b", "ba", "ba", "ba", "ba"]

    vs.

    "b ba baa baaa baaaaa".scan(/ba*/) # => ["b", "ba", "baa", "baaa", "baaaaa"]
  • /./ match any character

  • /^M/ matches its target if target exists at the beginning of a line in a string

    str = "Regexes are badass.\nI wonder if strings are jealous?\nRegexes are king."
    str.scan(/^Regexes/) # => ["Regexes", "Regexes"]
  • a{3,} 3 or more of 'a' so in 'haaapppy go lucky bunny rabbit' this will select 'aaa', but in 'haaaapppy go lucky bunny rabbit' you will get 'aaaa'. you can change this number to select whatever you want, however if you use 1 as in => a{1,} this will select every combination of 'a' so 'happpy go lucky bunny raaaabbit1' => 'a','aaaa'

  • \z Matches targets (".." in example) starting from the end of the string and working backwards from there.

    "Joshua Mejia [7:58 PM]"[(/..\z/]) => "M]"

    vs.

    "Joshua Mejia [7:58 PM]"[(/...\z/]) => "PM]"
  • \s Any whitespace character If using scan, will identify the character before or after. For example, string = "whatup yo". If you enter this: string.scan(/p\s/), you will get ["p "]. Does not account for more than one empty space.

  • \W Any non-word character: Finds spaces, commas, colons, etc - things that wouldn't be part of a word. This does find apostraphe's also, so look out for contractions!

  • (...) Capture an item in the string and then use . to represent any place your want around the string

  • \w Any word character (letter, number, underscore)

    "Joshua Mejia [7:58 PM]".scan(/\W/) => [" ", " ", "[", ":", " ", "]"]
    "Joshua Mejia [7:58 PM]".scan(/.\W/) => ["a ", "a ", "7:", "8 ", "M]"]
  • \b Any word boundary

  • () group items together. Ruby will also "capture" what gets matched and let you access it with $1, $2, ...

    "HTTP/1.1 200 OK"[/(\S+) (\S+) (.+)/]  # => "HTTP/1.1 200 OK"
    $1                                     # => "HTTP/1.1"
    $2                                     # => "200"
    $3                                     # => "OK"
  • a{3,6} Between 3 and 6 of a

    "hellllo worllllld!".scan(l{3,6}) => ["llll", "lllll"]

    If there are more than the max, the regex grabs the maximum, leaving behind the remainder of the characters as such:

    "777777777".scan(7{3-6}) => ["777777", "777"]
  • The positive lookahead is designated by regex_to_match(?=regex_after). It returns a match when regex_to_match is followed by regex_after. The match does not return the regex searched for within the parentheses. For example:

"aXbXcXdY".scan(/.(?=X)/) #=> ["a", "b", "c"]
"aX bY c#4".scan(/.(?=\w)/) #=> ["a", "b"]
  • The negative lookahead is designated by regex_to_match(?!regex_after). It returns a match when regex_to_match is not followed by regex_after. Similar to the positive lookahead, the match does not return the regex searched for within the parentheses.
"aXbXcXdY".scan(/[a-d](?!X)/) #=> ["d"]
"aX bY c#4d5".scan(/[a-c](?!\w)/) #=> ["c"]

Still unknown

  • [abc] A single character of: a, b, or c
  • [^abc] Any single character except: a, b, or c `"aabbfbezabeas abrba acaoboalb".scan(/[^ab]/) => ["f", "e", "z", "e", "s", " ", "r", " ", "c", "o", "o", "l"]
  • [a-z] Any single character in the range a-z
  • [a-zA-Z] Any single character in the range a-z or A-Z
  • ^ Start of line
  • $ End of line
  • \A Start of string
  • \S Any non-whitespace character
  • \d Any digit str.scan(/\d/) found only the digits 0-9 in the string
  • \D Any non-digit str.scan(/\D/) found only the nondigits a-z in the string
  • (a|b) a or b
  • a? Zero or one of a
  • a* Zero or more of a
  • a+ One or more of a
  • a{3} Exactly 3 of a
  • a{3,} 3 or more of a

regular-expression-knowledge-we-have-discovered's People

Contributors

joshcheek avatar adamhundley avatar adriennedomingus avatar erinnachen avatar hhoopes avatar weilandia avatar brennanholtzclaw avatar damwhit avatar mdes41 avatar marinacor1 avatar

Stargazers

Abbi Christina Corcoran avatar

Watchers

 avatar James Cloos avatar  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.