psql parse string with no delimiter - regex

I kindly request regexp help parsing the following string:
154211202251206660
The out put needs to be formatted like this:
1-54211-20225-12066-60
The string length is always the same the the break points (-) are always the same.
Many thanks!
PostgreSQL 9.1.9

Use following regex with capturing group
^(\d)(\d{5})(\d{5})(\d{5})(\d{2})$
Regex explanation here.
Regex can be use with regex_replace()
regexp_replace(
TheColumn, '^(\d)(\d{5})(\d{5})(\d{5})(\d{2})$', '\1-\2-\3-\3-\4'
)

Related

Regular Expression to extract a string based on delimiter

I am trying to extract a substring from a string based on delimiter '.'(period). Can someone share your thoughts on how to do it using regexp_extract please. Thanks.
**
- Input:-
15.075
0.035
**
Output
075
035
From this answer, it appears that you can use parentheses to capture of the match, as you would in most regex systems. That is, match the whole ".[0-9]+", but only capture the numeric portion, by surrounding it with parentheses, like this:
select regexp_extract(input, r'\.([0-9]+)');
This says to match a period followed by one or more numbers, and to extract the numeric portion only. I think that the leading r marks that string as a regular expression, but I can't find documentation on it.
Reference: https://cloud.google.com/bigquery/query-reference?hl=en#regularexpressionfunctions
It seems that you will want to use REGEXP_EXTRACT
REGEXP_EXTRACT(number, r'\.(\d+)')

Regex for url route with query string

I am having hard time learning regex and honestly I have no time at the moment.
I am looking for a regex expression that would match url route with query string
What I need is regex to match population?filter=nation of course where nation can be any string.
Based on my current regex knowledge I have also tried with regex expression /^population\/(?P<filterval>\d+)\/filter$/ to match population/nation/filter but this does not work.
Any suggestion and help is welcome.
This does match only your first query string format:
population\?filter=[\w]+[-_]?[\w]+
Addiotionally it allows for - and _ as bindings between words. If you know, that your string ends right there, you can also add an $ to the end to mark it so.
If you know that the nation is only alphabetical characters, yu can use the simplified version:
population\?filter=[\w]+
Demo

Regex replace keeping part of a string and adding data

I have this file with thousands of records (more thank 300.000) and I have to replace all the occurrences of a particular string but keeping some of it.
I'll give you an example, the string would be
\123
\34565
\923
..etc
so basically I would have to convert these strings in to
'|''|'123'
'|''|'34565'
'|''|'923'
does anyone have a quick solution for this?
Many thanks
Try this -
Regex - \\(\d+)
Replace with - '|''|'\1'
Demo here
Use this regex:
\\(\d+)
You should use g(global) modifier to match all. So your final regex would become:
/\\(\d+)/g
and replace it with:
'|''|'$1'
Demo:http://regex101.com/r/yO3xQ6

RegEx: capture entire group content

I am writing a parser for some Oracle commands, like
LOAD DATA
INFILE /DD/DATEN
TRUNCATE
PRESERVE BLANKS
INTO TABLE aaa.bbb
( some parameters... )
I already created a regex to match the entire command. I am now looking for a way to capture the name of the input file ("/DD/DATEN" for instance here).
My problem is that using the following regex will only return the last character of the first group ("N").
^\s*LOAD DATA\s*INFILE\s*(\w|\\|/)+\s*$
Debuggex Demo
Any ideas?
Many thanks in advance
EDIT: following #HamZa 's question, here would be the entire regex to parse Oracle LOAD DATA INFILE command (simplified though):
^\s*LOAD DATA\s*INFILE\s*((?:\w|\\|/)+)\s*((?:TRUNCATE|PRESERVE BLANKS)\s*){0,2}\s*INTO TABLE\s*((?:\w|\.)+)\s*\(\s*((\w+)\s*POSITION\s*\(\s*\d+\s*\:\s*\d+\s*\)\s*((DATE\s*\(\s*(\d+)\s*\)\s*\"YYYY-MM-DD\")|(INTEGER EXTERNAL)|(CHAR\s*\(\s*(\d+)\s*\)))\s*\,{0,1}\s*)+\)\s*$
Debuggex Demo
Let's point out the wrongdoer in your regex (\w|\\|/)+. What happens here ?
You're matching either a word character or a back/forwardslash and putting it in group 1 (\w|\\|/) after that you're telling the regex engine to do this one or more times +. What you actually want is to match those characters several times before grouping them. So you might use a non-matching group (?:) : ((?:\w|\\|/)+).
You might notice that you could just use a character class after all ([\w\\/]+). Hence, your regex could look like
^\s*LOAD DATA\s*INFILE\s*([\w\\/]+)\s*$
On a side note: that end anchor $ will cause your regex to fail if you're not using multiline mode. Or is it that you intentionally didn't post the full regex :) ?
Not tested but...
^\s*LOAD DATA\s*INFILE\s*(\S+)\s*$

Postgresql regex replace text field

Hi guys I have question about regex, can you help me extract date from text like:
Start 20130918 14:35:00
I wan extract 20130918 only from text.
I've tried something like this:
regexreplace(Start('\s+\w{5}\d{8}\s',''))
Better to use substring than regex_replace for this type of problem:
select substring('Start 20130918 14:35:00' from '[0-9]{8}')
You can use the following expression for matching the line:
Start\s+([0-9]{8})\s.*
and then a replacement string: \1.
NOTE: You might need to double escape each backslash if you are passing the expression as a string, so you might need to use:
Start\\s+([0-9]{8})\\s.*
EDIT: You can use the following statement:
SELECT regexp_replace('Start 20130918 14:35:00','Start\s+([0-9]{8})\s.*','\1')
and here is an SQL Fiddle Demo