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','|','.');
Related
I'm creating a javascript regex to match queries in a search engine string. I am having a problem with alternation. I have the following regex:
.*baidu.com.*[/?].*wd{1}=
I want to be able to match strings that have the string 'word' or 'qw' in addition to 'wd', but everything I try is unsuccessful. I thought I would be able to do something like the following:
.*baidu.com.*[/?].*[wd|word|qw]{1}=
but it does not seem to work.
replace [wd|word|qw] with (wd|word|qw) or (?:wd|word|qw).
[] denotes character sets, () denotes logical groupings.
Your expression:
.*baidu.com.*[/?].*[wd|word|qw]{1}=
does need a few changes, including [wd|word|qw] to (wd|word|qw) and getting rid of the redundant {1}, like so:
.*baidu.com.*[/?].*(wd|word|qw)=
But you also need to understand that the first part of your expression (.*baidu.com.*[/?].*) will match baidu.com hello what spelling/handle????????? or hbaidu-com/ or even something like lkas----jhdf lkja$##!3hdsfbaidugcomlaksjhdf.[($?lakshf, because the dot (.) matches any character except newlines... to match a literal dot, you have to escape it with a backslash (like \.)
There are several approaches you could take to match things in a URL, but we could help you more if you tell us what you are trying to do or accomplish - perhaps regex is not the best solution or (EDIT) only part of the best solution?
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
I am rather new to using vim with regular expressions and I need to count specific entries in a .csv file The entries are in this form:
9,1,8-Mar-11,high,A2,mid,500,1000,0.143494345,0.153521446,1121.386992,409.6833333,,
9,2,8-Mar-11,high,A2,mid,500,1000,0.180015537,0.256840072,1190.977918,420.8229933,1,
9,3,8-Mar-11,high,A2,mid,500,1000,0.250273568,0.16378268,1061.417761,419.1692065,,1
I need to count the number of 8-Mar-11, A2 conditions which have either ,, or ,1 (,|1) or (,|1),1 on the end of the lines.
here is the regular expression that i use in vim to get some count data:
:%s/.*8-Mar-11.*A2.*,,1$//gn
What I would like to know is there a way to use either in vim? like:
:%s/.*8-Mar-11.*A2.*,1\(,|1\)//gn
Any advice or help is greatly appreciated!
VIM regexes are weird and confusing. You have to escape the | as \| for or
When you're composing a regular expression which is searching for one character or another, alternation using pipes is overkill. Use a character class instead:
:%s/.*8-Mar-11.*A2.*,1[,1]//gn
Need some help in Notepad++
Example how it looks at the moment
http://www.test.com/doc/rat.rar">rat.rar
http://www.test.com/down/ung.rar">ung.rar
http://www.test.com/read/add.rar">add.rar
......
How I want it (just remove after ">....rar)
http://www.test.com/doc/rat.rar
http://www.test.com/down/ung.rar
http://www.test.com/read/add.rar
Its a list about 1000 lines. So help would be nice
Use the following expression:
">[^.]+\.rar
Explanation:
"> # literal `"` followed by literal `>`
[^.]+ # any character that is not a `.`, repeated at least once
\. # literal `.` character
rar # literal string `rar`
Note: a couple of other answers pointed out that just ">.* will work. This is true, because Notepad++ doesn't appear to support multi-line regular expressions, even with [\s\S]+. Either way will work so it's personal preference. The regex I gave in this answer is very verbose and would reduce the likelihood of false positives. ">.*, on the other hand, is shorter.
In regexp mode , replace pattern ">.* with empty string.
">.*
Search for this and replace with nothing.
Your search string should be ">.+\.rar, and you can just blank out the replace box. This should do the job.
Also, check that you've got regex selected at the bottom of the replace box ;)
If you put this in find ".* and nothing in replace, that should do what you're looking for.
Remember to check that you've got regex selected at the bottom of the replace box.
Flick the "regular expression" radio button and then use this for your FIND:
">[a-z]+\.[a-z]+
Then just put empty space for your REPLACE and replace all.
Use -
Find What : (.*)">(.*)
Replace With : \1
And check Regular expression option at the bottom of the dialog.
I would like to match either any sequence or digits, or the literal: na .
I am using:
"^\d*|na$"
Numbers are being matched, but not na.
Whats my mistake?
More info: im using this in a regular expression validator for a textbox in aspnet c#.
A blank entry is ok.
It's because the expression is being read (assuming PCRE):
"^\d*" OR "na$"
Some parentheses would take care of that in a jiff. Choose from (depending on your needs):
"^(\d+|na)$" // this will capture the number or na
"^(?:\d+|na)$" // this one won't capture
Cheers!
The | operator have a higher precedence than the anchors ^ and $. So the expression ^\d*|na$ means match ^\d* or na$. So try this:
^(\d*|na)$
Or:
^\d*$|^na$
Perhaps ^(?:\d*|na)$ would be better. What language/engine? Also, please show the input and, if possible, the snippet of the code.
Also, it is possible that you aren't matching "na" because there is a new line after it. The digits wouldn't be affected because you did not specify a $ anchor for them.
So, depending on the language and how the input is acquired, there might be new-line between "na" and the end of the string, and $ won't match it unless you turn on multi-line match (or strip the string of the new line).
This may not be the best or most elegant way to fix it, but try this:
"^\d*|[n][a]$"