This question already has answers here:
Match exact string
(3 answers)
Closed 3 years ago.
all.
I'm querying a Postgres 9.3 database looking for a specific pattern in a field:
SELECT
P.id, P.processo_id, PR.num_formated
FROM
publications P
INNER JOIN processos PR ON PR.id=P.processo_id
WHERE
--The first numeric sequence must be exact 4 digits length, so the initial \d{4}
PR.num_formated ~ '\d{4}[\.\-\\]\d{2}\.\d{4}\.\d{1}\.\d{2}\.\d{4}'
AND
P.id=781291700 --Just to force a specific record
where PR.num_formated is defined as "character varying(255)". After being executed, the query returns:
P.id P.processo_id PR.num_formated
781291700 502707245 20190001418-14.1998.8.05.0103
My question is: Why is Postgres "ignoring" the first \d? Is there any specificity in the form it interprets the regular expressions that differ from the "traditional/regular/orthodox/whatever" way, since the same regex works perfectly in another part of my system, but using a ruby code?
Thanks in advance
Walid
The first 7 chars are ignored because the query finds the pattern as a part of the string. If you want to match the whole string use ^ and $ constraints.
'^\d{4}[\.\-\\]\d{2}\.\d{4}\.\d{1}\.\d{2}\.\d{4}$'
Related
This question already has answers here:
How can I match "anything up until this sequence of characters" in a regular expression?
(15 answers)
Closed 3 years ago.
I have strings that are concatenations of airline codes/flightnumbers, separated with ;. The airline code and flight number are either separated by a space or -. So some examples are:
AA-1234;UA 243;EK 23;
9W 23;B6-134
Now I want to grab the airline codes from this.
I came up with the following regex: [a-zA-Z0-9]{2}[ -]. This works to grab the airline codes but also includes the airlinecode-flightnumber separator. How would I adjust my regex to not include this?
[a-zA-Z0-9]{2}(?=[ -])
See it in action here
This question already has answers here:
How can I validate an email address using a regular expression?
(79 answers)
Closed 4 years ago.
Trying to create custom email validation for below rules
The local part can be up to 64 characters in length and consist of any combination of alphabetic characters, digits, or any of the following special characters:
! # $ % & ‘ * + – / = ? ^ _ ` . { | } ~
The period character (".") is valid for the local part subject to the following restrictions: A. it is not the first or last character
B. two or more consecutive periods
top level domains cannot be all numeric
hyphens cannot be the first or last character
^([a-zA-Z0-9!#\$%&‘*+/\=\?\^_'`}{\|~-][.]?)#[a-zA-Z0-9]+(?:(.)\0?(?!\1))[a-zA-Z0-9-]*[a-zA-Z0-9]+(.[a-zA-Z0-9]{2,63})+$
First part (before # )is good but unable to place
two or more consecutive periods
hyphens cannot be the first or last character
for example
leela.test#te-st.gm-ail.com(correct)
leela.test#te-st..gm-ail.com(incorrect)
leela.test#.te-st.gm-ail.com(incorrect)
leela.test#-te-st.gm-ail-.com(incorrect)
leela.test#.te-st.gm-ail-.com(incorrect)
leela.test#test.gmail.com(correct)
leela#gmail.com(correct)
leela#test.gm-ail.com(correct)
Please help.
[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)*\#[a-zA-Z0-9]+\-[A-Za-z0-9]+\.[a-zA-Z0-9]+\-[A-Za-z0-9]+\.com
This question already has answers here:
Getting the text that follows after the regex match
(5 answers)
Closed 4 years ago.
I have a line containing
[India,sn_GB] Welcome : { Name:{Customer1},Place:{Mumbai},}
I want to print the entire line after sn_GB] in splunk, which is
Welcome : { Name:{Customer1},Place:{Mumbai},}
I used the below regular expression:
(?<=sn_).*?$
But it prints, along with GB] like GB] Welcome : { Name:{Customer1},Place:{Mumbai},}.
In the word sn_GB, sn_ is constant and the rest two letter will vary, like GB, LB, KB, TB as such.
Please help me in correcting the regular expression.
Thanks
This will give the correct result in case sn_GB is constant.
(?<=sn_GB).*?$
If GB is not constant you can go for:
(?<=sn_...).*?$
I understand your question now.
Country codes are always 2 letters.
i'd use
(?<=sn_..\]\ ).*$
but you could use
(?<=sn_[A-Z]{0,5}\]\ \s*).*?$
(?<=sn_....).*$
is the simplest, as it will just grab 4 characters after, if it's always 2 letters for country code, and then a closing bracket and a space
This question already has answers here:
How to extract a value from a URL query string in C#?
(4 answers)
Closed 4 years ago.
I'm trying to capture a group that has a variety of different forms.
cardType=A&Return=True
cardType=AbC321
Return=False&cardType=C
My current regex is:
cardType=(?<Card Type>.*)&?
This currently captures the 2 and 3, but not in 1 as it also captures Return in that case.
If I do instead:
cardType=(?<Card Type>.*?)&
Then it correctly captures 1, but not 2 and 3.
How do I write a regex that captures it in all 3 cases?
Use:
cardType=(?<CardType>[^&\s]*)
Demo
Two important changes:
removed space form group name
replaced .*)&? with [^&\s]*)
The second change is more important. When you do .*&?, then &? is never captured, because .* takes the & sign. As in most cases it's better to limit the repetition by limiting scope of accepted charactes to [^&\s] - anything but whitespace or ampersand
This question already has answers here:
What's a Regex pattern for 3 consecutive digits increasing or decreasing
(3 answers)
Closed 6 years ago.
i would like to ask if it is possible to get a regex to check for subsequent alphabets or numbers in a string such as "abcd" or "1234" 4 characters in succession.
This is for password validation to check if a user is trying to enter a password like "abcd1234"
Thank You
Based on the answer Match increasing/decreasing sequences using regex, this pattern does close. Through programming you still need to get the first capturing group \1 and check whether its size is equal to 4. Here you need to set the multi line mode, to allow $ match the end of line.
This is because this regex is capturing all possible groups, however the groups you want has the length of 4, so it should not be a problem.
(?x)
(
(?:a(?=b|$))?
(?:b(?=c|$))?
(?:c(?=d|$))?
(?:d(?=e|$))?
(?:e(?=f|$))?
(?:f(?=g|$))?
(?:g(?=h|$))?
(?:h(?=i|$))?
(?:i(?=j|$))?
(?:j(?=k|$))?
(?:k(?=l|$))?
(?:l(?=m|$))?
(?:m(?=n|$))?
(?:n(?=o|$))?
(?:o(?=p|$))?
(?:p(?=q|$))?
(?:q(?=r|$))?
(?:r(?=s|$))?
(?:s(?=t|$))?
(?:t(?=u|$))?
(?:u(?=x|$))?
(?:x(?=z|$))?
[a-z]?
|
(?:0(?=1|$))?
(?:1(?=2|$))?
(?:2(?=3|$))?
(?:3(?=4|$))?
(?:4(?=5|$))?
(?:5(?=6|$))?
(?:6(?=7|$))?
(?:7(?=8|$))?
(?:8(?=9|$))?
\d?
)
On this link you can see the live regex: https://regex101.com/r/xxED4s/2