Extract specific values (n digit long and starting with a number) PSQL - postgresql

I have two string like this and I want to extract 6 digits starting from "2007" using postgresql. Please help
"472a62b98b-2004-07-l-de_k-de_n-email_t-cus_200703"
"21d6bd96f2-2004-04-l-de_c-de_m-email_t-cus_200705-b"
Results would be:
"200703"
"200705"

You can use substring() with a regex:
substring(the_column from '2007[0-9]{2}')
This extracts the first substring that starts with 2007 and is followed by (exactly) two digits.

Related

postgres substring split text using regex

I am having following string pattern and I want to split the text into 4 fields.
NIFTY21JUN11100CE --> NIFTY, 21JUN, 11100, CE
In above string, only 2 string formats are constant. For ex: 21JUN represents year and month and it is constant 5 character representation. Before that represent name which can be any number of characters. I think regex will be like (([1-2][0-9]))(JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)
last 2 characters are constant and its value can be either PE|CE. value between 21JUN and CE|PE represent strike price and it is always numeric but can be any number of digits.
Now I want them to be split into 4 fields and struggling to get the regex. Is anyone familiar with Postgres command for this requirement?
You can use SELECT regexp_match('NIFTY21JUN11100CE','^(\D+)(\d{2}[A-Z]{3})(\d+)(PE|CE)$');
Step by step:
^ Beginning of the string
( start capture
\D+ more than zero non-digit chars
) end capture
( start capture
\d{2} exactly 2 digits
[A-Z]{3} exactly 3 chars in the range from A to Z
) end capture
( start capture
\d+ more than zero digit chars
) end capture
( start capture
PE|CE one of 'PE' or 'CE'
) end capture
$ end of the string
The year-month regexes from your question using character classes [1-2][0-9] and alternations (JAN|FEB|...) are a little bit more strict and could also be used.

Encode a Date and a four digit number into a string with max 8 characters

I have a datetime and a four digit number and I need to encode this into a 8 character case insensitive ASCII string.
The four digit number is not actually an arbitrary number, but there are only a certain numbers (about 20 or so) of the form (2513, 2595, 2579, ...).
My current approach is to use Base36 encoding. Further, I have a dictionary for the four digit numbers that maps like this:
2513 -> '00'
2595 -> '01'
...
The first two characters of the resulting string are used for this. The remaining six characters are used for encoding a unix timestamp with seconds stripped (I only need seconds resolution) in Base36.
So, (2513, 07.01.2015) maps to '000E3HEU'.
My question is, if anyone can think of an even more compact encoding?

Format postgres numeric like money ($0.20)

I have a numeric column that I'm trying to format like currency, but I can't seem to get the format right. I currently have:
to_char(my_column, 'fml9999999999999999999D9999999999999999999')
but it outputs
$.2
If I remove the 'fm' modifier, it outputs:
$ .2000000000000000000
How would I go about getting it to preserve at least 1 digit on the left, and at least 2 digits on the right while removing all the rest of the trailing 0's?
Figured it out: the trick is to use 0's where you want it to preserve the digits:
to_char(my_column, 'fm9999999999999990D00')

How to use numbers as delimiters in MATLAB strsplit function

As the title suggests I'm looking to detect where the numbers are in a string and then to just take the substring from the larger string. EG
If I have say zero89 or eight78, I would just like zero or nine returned. When using the strsplit function I have:
strsplit('zero89', but what do I put here?)
Interested in regexp that will provide you more options to explore with?
Extract numeric digits -
regexp('zero89','\d','match')
Extract anything other than digits -
regexp('zero89','\d+','Split')
strsplit('zero89', '\d', 'DelimiterType', 'RegularExpression')
Or just using regexp:
regexp('zero89','\D+','match')
I got the \D+ from here
Assuming you mean this strsplit?
strsplit('zero89', '8')

Trouble determining the pattern for NSRegularExpression...?

i am relatively new to NSRegularExpression and just can't come up with a pattern to find a string within a string....
here is the string...
##$294#001#[12345-678[123-456-7#15665#2
I want to extract the string..
#001#[12345-678[123-456-7#
for more info I know that there will be 3 digits(like 001) between two # 's and 20 characters between the last two # 's..
I have tried n number of combinations but nothing seem to work. any help is appreciated.
How about something like this:
#[0-9]{3}#.{20}#
If you know that the 20 characters will always consist of digits, [ and -, your pattern would become:
#[0-9]{3}#[0-9\[\-]{20}#
Be careful with the backslashes: When you use create the pattern with a string literal (#"..."), you need to add an extra backslash before each backslash.
You can test NSRegularExpression patterns without recompiling each time by using RegexTester https://github.com/liyanage/regextester