Regex Help - Named capture group that ends in two possible ways [duplicate] - regex

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

Related

Regular Expression Match Groups [duplicate]

This question already has answers here:
Regular expression to stop at first match
(9 answers)
Closed 2 years ago.
I have the following regular expression:
(.*(\d*)(-)(\d*).*)
It correctly matches the following string:
Court 19-24
However, the second group is empty - Group 1: Court 19-24, Group 2: [empty], Group 3: -, Group 4: 24
What is wrong with my regular expression that the second group doesn't contain 19?
Maybe you are looking for something like this?
let re = /.* (\d+)(-)(\d+).*/;
let str = 'Court 19-24';
var match = re.exec(str);
console.log('group 0:', match[0])
console.log('group 1:', match[1])
console.log('group 2:', match[2])
console.log('group 3:', match[3])
Group indexes annotated
(.(\d)(-)(\d*).*)
1 2 3 4
Not sure what you expected here?
To match 19 and 24 into two separate groups, use something like this:
(\d+)-(\d+)
To also match the court name into a group, a non-greedy star works.
(.*?) (\d+)-(\d+)
I bet your initial regex was more like this:
(.*(\d*)(-)(\d*).*)
This will behave closer to what you describe, because
. also matches digits, i.e. .* will go up to the -, and
* is not required to match anything at all, i.e. \d* it will happily match zero digits, causing group 2 to be empty.
Lessons:
Don't use * when you expect at least something to be there. Prefer + in this case.
Be wary of the greedy star, especially when used with the unspecific . it can match things you did not intend.
You don't have to put parenthesis around everything in regular expressions. Only add groups around things you want to extract (i.e. "capture"), or around things you want to match/fail/repeat as one (i.e. "make atomic").

Regular expressions in Postgres [duplicate]

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}$'

regex to check for subsequent characters [duplicate]

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

Regular Expression for US and other format zip [duplicate]

This question already has answers here:
What is the ultimate postal code and zip regex?
(20 answers)
Closed 9 years ago.
Am working on regular expression for following formats of zip in groovy
Includes a letter (L12345)
Includes a dash plus 4 more numbers (77056-1234)
Includes spaces (77056 1234)
I have this "^\d{5}(-\d{4})?\$" but its not matching the required formats. Could anyone please help me?
^\d{5}(?:[-\s]\d{4})?$
^ = Start of the string.
\d{5} = Match 5 digits (for condition 1, 2, 3)
(?:…) = Grouping
[-\s] = Match a space (for condition 3) or a hyphen (for condition 2)
\d{4} = Match 4 digits (for condition 2, 3)
…? = The pattern before it is optional (for condition 1)
$ = End of the string.
This is from the following question, hope it helps
regex for zip-code
For the optiona startingil letter use
[A-Z]?
to make the letter optional. {1} is redundant. (Of course you could also write [A-Z]{0,1} which would mean the same, but that's what the ? is there for.)
I think it should go after the ^ but haven't had a chance to test

difference between 2 regex strings [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 4 years ago.
What is the difference in what these 2 are doing? Thx
var m = document.referrer.match(/\&cd=([\d]*)/);
and
var m = document.referrer.match(/cd=(.*?)&/);
Which one is more efficient and effective?
/\&cd=([\d]*)/ - matches any string starting with an "&cd=" followed by any zero or more decimal digits. The first capture group is the decimal digits.
/cd=(.*)&/ - matches any string starting with an "cd=" followed by zero or more characters up to and including the first "&". The first capture group is all characters between "cd=" and "&".
They are similar, but not equivalent. Which one you should use depends on your exact needs. Judging from your comment, it sounds like you want to use:
var m = document.referrer.match(/[?&]cd=(\d+)/);