Regex Operations in BigQuery

Developer building products for Fortune 500!
Search for a command to run...

Developer building products for Fortune 500!
No comments yet. Be the first to comment.
Writing code is both an art and a science, but it can be a daunting task. As developers, we invest a significant amount of time crafting intricate lines of code. However, what if I told you that a simple habit of adding code comments can revolutioniz...

In the fast-paced world of software development, clear and comprehensive code documentation is like a treasure map 🗺️ that helps fellow developers navigate your codebase. In this blog post, we'll embark on an exciting journey to discover the power o...

As a developer, you understand that writing clean and efficient code is essential for maintaining and scaling your Node.js applications. Code quality not only affects the performance and stability of your application but also makes it easier for your...

Serverless computing is one of the fastest-growing trends of the decade. Cloud Functions is the Function-as-a-Service (FaaS) offerings from Google. Serverless minimizes DevOps complexity while maintaining high elasticity.With Cloud Functions, you onl...

"Kubernetes is awesome, I would love to see auto-healing in my systems” Working with new frameworks is fascinating, but it also births a few questions. “Are enterprises actually using it?” “Is it really worth the switch? my current solution works fin...

BigQuery is taking the analytics industry by storm. One of the most important use cases is to extract and transform parts of a string. Below are some of the powerful constructs provided by BigQuery.
BigQuery provides the following constructs for regex operations.
| Construct | Arguments | Use |
| REGEXP_CONTAINS | REGEXP_CONTAINS(value, regexp) | Checks if the string contains the regex |
| REGEXP_EXTRACT | REGEXP_EXTRACT(value, regexp[, position[, occurrence]]) | Extract the substring that matches the regex. |
| REGEXP_EXTRACT_ALL | REGEXP_EXTRACT_ALL(value, regexp) | Return all substrings that match the regex |
| REGEXP_INSTR | REGEXP_INSTR(source_value, regexp [, position[, occurrence, [occurrence_position]]]) | Return 1-based index for the first occurrence of the regex |
| REGEXP_REPLACE | REGEXP_REPLACE(value, regexp, replacement) | Replace all substrings in value |
Source: Google Cloud Platform
Let’s create a simple table with some string data to play around with.
-- Create Store
CREATE OR REPLACE TABLE `gcp-tour-276710.thefellowcoder.store`
(
date DATE,
details STRING,
address STRING
)
-- Add values
INSERT INTO `gcp-tour-276710.thefellowcoder.store`
VALUES
('2021-07-14', '<h1>Nike Shoes</h1><p>Base: $450 Addon: $20</p><p>Support email: help@nike.com</p>','Los Angeles, California 90001 '),
('2021-06-13', '<h1>Rebook Shoes</h1><p>Base: $150 Addon: $10</p><p>Support email: help@rebook.com</p>','Sacramento, California 94203 '),
('2021-07-11', '<h1>Adidas Shoes</h1><p>Base: $350 Addon: $30</p><p>Support email: help@adidas.com</p>','Beverly Hills, California 90209 '),
('2021-07-11', '<h1>Carnival All Rounder Shoes</h1><p>Base: $250 Addon: $0</p>','Beverly Hills, California 90209 ')
Let's see each of the constructs in action
-- All stores that have a support email
select * from `gcp-tour-276710.thefellowcoder.store`
where REGEXP_CONTAINS(details, r'\S+@\S+\.\S+')
-- Extract Product Names
select REGEXP_EXTRACT(details, r'<h1>([^<]+)<\/h1>')
from `gcp-tour-276710.thefellowcoder.store`
-- Extract all prices
select REGEXP_EXTRACT_ALL(details, r'\$(.\d+)')
from `gcp-tour-276710.thefellowcoder.store`
-- Get index of email
select details, REGEXP_INSTR(details, r'\S+@\S+\.\S+')
from `gcp-tour-276710.thefellowcoder.store`
-- Replace all email-ids
select REGEXP_REPLACE(details, r'\S+@\S+\.\S+', 'admin@thefellowcoder.com') from `gcp-tour-276710.thefellowcoder.store`
This is one of the most common use cases wherein we must extract a substring that matches a regex.
-- Extract product names from all details
select REGEXP_EXTRACT(details, r'<h1>([^<]+)<\/h1>')
from `gcp-tour-276710.thefellowcoder.store`
Let’s say you want to sanitize the string and remove all special characters from the string.
-- remove special characters
select details,REGEXP_REPLACE(details, r'[^0-9a-zA-Z]+',' ')
from `gcp-tour-276710.thefellowcoder.store`
check for multiple regex patterns for a string
-- check for pincodes starting with 90/94
select address,REGEXP_EXTRACT(address, r'9[0|4]\d+') as pincode
from `gcp-tour-276710.thefellowcoder.store`
| String | Regex | Output |
| <h1>Hello World!</h1> | <h1>([^<]+)<\/h1> | Hello World! |
| <p>Support email: thefellowcoder@gmail.com</p> | \w+@\w+\.\w+ | thefellowcoder@gmail.com |
| Rebook ShOes | (?i)shoes | ShOes |
| This,contains$special\characters | [^0-9a-zA-Z]+ | This contains special characters |
| String needs to be trimmed. | ^(\s+).*?(\s+)$ | String needs to be trimmed. |