Postgresql regex replace text field - regex

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

Related

Notepad++ Regex to find group of lines with condition

Given this example text:
<abr:rules>
<abr:ruleTypeDefinition>
<abr:code>ABB</abr:code>
<abr:ownership>
<abr:owner organization="NT" application="DCS" subapplication="FM"/>
...lines...
...........
</abr:rules>
<abr:rules>
<abr:ruleTypeDefinition>
<abr:code>ADE</abr:code>
<abr:ownership>
<abr:owner organization="NT" application="DCS" subapplication="CM"/>
...lines...
...........
</abr:rules> (end of group)
I would like to find and remove all that goes from <abr:rules> to </abr:rules> with the condition that subapplication IS NOT "CM". Organization and application are the same, <abr:code> it's any string.
What I tried so far is
<abr:rules>\n<abr:ruleTypeDefinition>\n<abr:code>[a-zA-Z0-9]{3,}<\/abr:code>\n<abr:ownership>\n<.*"(FM|PSD|SSC)"\/>\n(?s).*?\n<\/abr:rules>\n
which works but only because I know the other subapplication names.
Is there any way to do it with Regex only ?
Try the following find and replace:
Find:
<abr:rules>((?!subapplication=).)*subapplication="(?!CM")[^"]+"((?!</abr:rules>).)*</abr:rules>
Replace:
(empty string)
Demo
Note: The above pattern will only work if you enable dot in Notepad++ to match newlines. If you don't want to do that, then you may use [\S\s] instead of dot.
You should not use regex for xml, you can read why here:
https://stackoverflow.com/a/1732454/3763374
Instead you can use some parser like Xpath

Regex to match the string until character occured, unless it's enclosed within quotes

Assuming a QueryString that looks like: $filter=Name eq "Demo"&project=10
I would like to use groups to get the value of the $filter.
This can be done with the following regex:
\$filter=(?P<group>[^&]*)
However, when the filter looks like $filter=Name eq "De&mo"&project=10 the Regex does not work anymore because it matches the & sign that enclosed within ".
How shoud this regex be adapted so that the full $filter is retrieved?
Kind regards
You can use this regex to match and consule all quoted string in the query parameter $filter:
\$filter=((?:[^"&]*"[^"]*")*[^"&]*)
RegEx Demo

psql parse string with no delimiter

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'
)

How to select text between greater than and less than with an additional slash

I'm trying to select text between ></ . Example below I want "text"
>text</
but I'm unable to do so.
tried the following but it doesn't like the slash at the end of the regex
\>(.*?)\<\
I'm trying to do this in TextPad. How is this supposed to be done?
I'm ultimately wanting to delete all text between these two characters so all I'm left with is something like: <element></element>
RegEx wise, you can use 3 groupings and for the replace only use the first and 3rd group: \1\3.
Find: (>)(.*)(</)
Replace: \1\3
Try doing:
\>(.*?)\<\/
The regex that you were trying would actually have given error because you had a \ and nothing after that.
You are close.. use the following:
(>).*?(<\/)
And replace with \1\2
See DEMO
OR
You can use lookbehind and lookaheads:
(?<=>)(.*?)(?=<\/)
And replace with '' (empty string)
See DEMO

Postgres regex and special characters

I am using posgresql, and I have encountered an issue with regular expressions and special characters.
select regexp_replace('asdf|asdf','|','.');
This function returns:
.asdf|asdf
Desired output:
asdf.asdf
How I can solve it? Please help :)
| is a special character in regex syntax called alternation, it means "or".
Your regex is selecting the empty string at the beginning of your string.
Try escaping it:
select regexp_replace('asdf|asdf','\|','.');
As #pozs underlined, for this particular task it is way more suited to use a simple replace:
select replace('asdf|asdf','|','.');