Cheatsheet for the Regex Cheatsheet, Part V: Quantifiers

Intro

I was recently doing a code challenge for a job interview that required me to strip out all nonalphabetic characters. “Ah! I should use Regular Expressions for this!” I thought in triumph, impressed that I even knew what regular expres…


This content originally appeared on DEV Community and was authored by Analogy | Absence | Example

Intro

I was recently doing a code challenge for a job interview that required me to strip out all nonalphabetic characters. "Ah! I should use Regular Expressions for this!" I thought in triumph, impressed that I even knew what regular expressions were. That fleeting moment of glory faded once I decided to brush up on regular expressions and landed on the encouragingly-named Regular Expressions Cheatsheet. I had no idea how to use it!

So, for people like me, here is a Cheatsheet for the Regular Expressions Cheatsheet, Part V: Quantifiers

Alt Text

What's an Quantifier?

A quantifier finds a sequence of characters to match. It also can be used to find a sequence of expressions to match, but I'm gonna keep it simple here and focus on sequences of characters.

Anatomy of a regular expression

  1. Forward slashes go on either end like so: /something/
  2. Add g for "global" at the end to find every instance, like so: /something/g
  3. Add m to "multi line" to the beginning/end of each line, not just the beginning/end of each string, like /something/g or /something/gm

Quantifiers

* 0 or more instances of a character
  • ro*ar is used in /ro*ar/ to find the following: The lion said roar rooar roooar roooooooar!
  • Example on regex101.com
  • Example in Javascript:
let sentence = "The lion said roar rooar roooar roooooooar!";
let regex = /ro*ar/;
let found = sentence.match(regex);
console.log(found); // [
  'roar',
  index: 14,
  input: 'The lion said roar rooar roooar roooooooar!',
  groups: undefined
]
+ 1 or more instances of a character
  • ro+ar is used in /ro+ar/ to find the following: The lion said roar rooar roooar roooooooar!
  • Example on regex101.com
  • Example in Javascript:
let sentence = "The lion said roar rooar roooar roooooooar!";
let regex = /ro+ar/;
let found = sentence.match(regex);
console.log(found); // [
  'roar',
  index: 14,
  input: 'The lion said roar rooar roooar roooooooar!',
  groups: undefined
]
? 0 or 1 instance of a character
  • ro?ar is used in /ro+ar/ to find the following: The lion said roar rooar roooar roooooooar!
  • Example on regex101.com
  • Example in Javascript:
let sentence = "The lion said roar rooar roooar roooooooar!";
let regex = /ro?ar/;
let found = sentence.match(regex);
console.log(found); // [
  'roar',
  index: 14,
  input: 'The lion said roar rooar roooar roooooooar!',
  groups: undefined
]
{N} N instances of a character
  • {3} is used in /ro{3}ar/ to find the following: The lion said roar rooar roooar roooooooar!
  • Example on regex101.com
  • Example in Javascript:
let sentence = "The lion said roar rooar roooar roooooooar!";
let regex = /ro{3}ar/;
let found = sentence.match(regex);
console.log(found); // [
  'roooar',
  index: 25,
  input: 'The lion said roar rooar roooar roooooooar!',
  groups: undefined
]
{N,} At least N instances of a character
  • {3,} is used in /ro{3,}ar/ to find the following: The lion said roar rooar roooar roooooooar!
  • Example on regex101.com
  • Example in Javascript:
let sentence = "The lion said roar rooar roooar roooooooar!";
let regex = /ro{3,}ar/;
let found = sentence.match(regex);
console.log(found); // [
  'roooar',
  index: 25,
  input: 'The lion said roar rooar roooar roooooooar!',
  groups: undefined
]
{N,M} Between N and M instances of a character
  • {2,4} is used in /ro{2,4}ar/ to find the following: The lion said roar rooar roooar roooooooar!
  • Example on regex101.com
  • Example in Javascript:
let sentence = "The lion said roar rooar roooar roooooooar!";
let regex = /ro{2,4}ar/;
let found = sentence.match(regex);
console.log(found); // [
  'rooar',
  index: 19,
  input: 'The lion said roar rooar roooar roooooooar!',
  groups: undefined
]


This content originally appeared on DEV Community and was authored by Analogy | Absence | Example


Print Share Comment Cite Upload Translate Updates
APA

Analogy | Absence | Example | Sciencx (2021-05-10T17:52:30+00:00) Cheatsheet for the Regex Cheatsheet, Part V: Quantifiers. Retrieved from https://www.scien.cx/2021/05/10/cheatsheet-for-the-regex-cheatsheet-part-v-quantifiers/

MLA
" » Cheatsheet for the Regex Cheatsheet, Part V: Quantifiers." Analogy | Absence | Example | Sciencx - Monday May 10, 2021, https://www.scien.cx/2021/05/10/cheatsheet-for-the-regex-cheatsheet-part-v-quantifiers/
HARVARD
Analogy | Absence | Example | Sciencx Monday May 10, 2021 » Cheatsheet for the Regex Cheatsheet, Part V: Quantifiers., viewed ,<https://www.scien.cx/2021/05/10/cheatsheet-for-the-regex-cheatsheet-part-v-quantifiers/>
VANCOUVER
Analogy | Absence | Example | Sciencx - » Cheatsheet for the Regex Cheatsheet, Part V: Quantifiers. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/10/cheatsheet-for-the-regex-cheatsheet-part-v-quantifiers/
CHICAGO
" » Cheatsheet for the Regex Cheatsheet, Part V: Quantifiers." Analogy | Absence | Example | Sciencx - Accessed . https://www.scien.cx/2021/05/10/cheatsheet-for-the-regex-cheatsheet-part-v-quantifiers/
IEEE
" » Cheatsheet for the Regex Cheatsheet, Part V: Quantifiers." Analogy | Absence | Example | Sciencx [Online]. Available: https://www.scien.cx/2021/05/10/cheatsheet-for-the-regex-cheatsheet-part-v-quantifiers/. [Accessed: ]
rf:citation
» Cheatsheet for the Regex Cheatsheet, Part V: Quantifiers | Analogy | Absence | Example | Sciencx | https://www.scien.cx/2021/05/10/cheatsheet-for-the-regex-cheatsheet-part-v-quantifiers/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.