Find IP range with RegEx - sql

I have an input of an IPV4 address and its prefix(from 1-32).
My output should be somthing like:
input:172.194.11.80/24
output: 172.194.11.0-172.194.11.255
I'm doing this via VERTICA (SQL)
My question is this something I should do with RegEx?
Or should I build a function that does this?

Related

REGEXP_SUBSTR with a URL

I have a string in which I'm trying to extract a URL from. When I run it on this RegEx site, it works fine.
The Regex Pattern is: http:\/\/GNTXN.US\/\S+
The message I'm extracting from is below, and lives in a column called body in my SQL database.
Test Message: We want to hear from you! Take our 2022 survey & tell us what matters most to you this year: http://GNTXN.US/qsx Text STOP 2 stop/HELP 4 help
But when I run the following in SQL:
SELECT
body,
REGEXP_SUBSTR(body, 'http:\/\/GNTXN.US\/\S+') new_body
FROM
table.test
It returns no value. I have to imagine it's something to do with the backslashes in the URL, but I've tried everything.
The new_body output should read as http://GNTXN.US/qsx
In mysql you just need to escape the \
select body, REGEXP_SUBSTR(body, 'http:\\/\\/GNTXN.US\\/\\S+') as new_body
from table.test;
new_body output:
http://GNTXN.US/qsx

How to extract the words inside the 2nd brackets using Regexp_extract in Bigquery?

I am having a textPayload column in bigquery table containing these values
textPayload
# User#Host: root[root] # [44.27.156.25] thread_id: 67301 server_id: 1220687984
I need to extract the username and host name as separate fields in the following fashion:
User:root Host:44.27.156.25,
All the values of this column will be containing the text as posted above
I am trying like this Select Regexp_Extract(textPayload, -> unable to get the Regex
I am new to regexp_extract and I am not able to extract the 2nd word which is the host:44.27.156.25,
Can anyone help me in extracting the Host name through a Regexp_extract ?
You could use the context of User# in the text payload, and that you want an IP address in square brackets, to find the content you want:
SELECT
textPayload,
REGEXP_EXTRACT(textPayload, r"\bUser#.*?\[(.*?)\]") AS User,
REGEXP_EXTRACT(textPayload, r"\[(\d+\.\d+\.\d+\.\d+)\]"
FROM yourTable;
Try regexp_extract_all with r'\[(.+?)\]':
select regexp_extract_all('# User#Host: root[root] # [44.27.156.25] thread_id: 67301 server_id: 1220687984', r'\[(.+?)\]')

Extract characters between a string and the first occurrence of something in BigQuery

I want to extract a set of characters between "u1=" and the first semi-colon using a regex. For instance, given the following string: id=1w54;name=nick;u1=blue;u2=male;u3=ohio;u5=
The desired regex output should be just blue.
I tested (?<=u1=)[^;]* on https://regex101.com and it works. However, when I run this in BigQuery, using regexp_extract(string, '(?<=u1=)[^;]*') , I get an error that reads "Cannot parse regular expression: invalid perl operator: (?<"
I'm confused why this isn't working in BQ. Any help would be appreciated.
You can use regexp_extract() like this:
regexp_extract(string, 'u1=([^;]+)')

How to replace part of a string in SQLite using Regex?

If i have the following string:
I WOULD LIKE TO USE REGEXP TO SOLVE THIS PROBLEM AND NOT THE DEFINED WORD
"123456abcd" and "123456"
I would like to find and replace where the numbers 123456 appear in that order, i know that regex has to be used in SQlite but I cannot seem to find which function to use (either REPLACE or UPDATE).
For example in the above - I would like to replace 123456 with the string "cheese" - then I would like the following:
"cheeseabcd" and "cheese"
I am stuck on how to solve this in SQL lite! everytime I make a change it changes the entire string and not just part of the string!
I don't see any regular expressions here. Just:
replace(col, '123456', cheese)

Apache Pig Latin using matches

I am trying with Apache Pig latin to FILTER url which matches particular pattern.
for ex:
**http://www.example.com/homePage.do?locale=en_US
I want to filter all the URL that matches
locale=en_US (or)
locale=en_CA
Please need help on resolving this.
You can use url matches '.*(locale=en_US|locale=en_CA).*'
It is good also to look at https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html when using regex in Pig