regex to find a substring with key value pair - regex

I want a regex for below scenario:
Input String:
"/eve/?tr=abcd&ay=123&test=123456789&e=event"
Output of regex should be (Search for attribute "test" and get the value against it: 123456789
I am using regex: [/test=([^\"]*)/, 1] but it returns string "123456789&e=event"
How to correct this regex?

You need to match everything up to the next &, not ":
[/test=([^&]*)/, 1]

Regexes aren't a magic wand that you wave at every programming problem that involves strings.
What language are you doing this in? Try to use existing tools instead of parsing your own with regexes. If you're using PHP, for example, use the parse_url function and then use explode to break apart the query string on = and &.

Try this regex !
You can extract anything before & .
&test=([^&]*)
I am also attach the output of the regex , you can verify it .

Related

Extract only the First set of numbers in a String

I have a string like below:
input = Test_8234_and_2345_end
REG_EXTRACT(input,'(\d+)',1)
I'm trying to create a regex that targets only the first set of numbers (i.e 8234) in the above string but it it returns none. what is wrong with my above code.
I'm not familiar with Informatica, but the following Regex pattern should work if you disable the greedy global modifier for the pattern:
(?<=_)[0-9]*
I used the regex101.com online regex calculator to test the pattern and it worked with Python.

Regex to get a count of strings that match the pattern

I want to count the number of occurrences of the below format of string in notepad++.
name="*" lastName="*"
(* represents it can be any name/lastName)
I am using the following regex
/name="([A-Z])\w" lastName="([A-Z])\w"
I am not able to figure out how to give hard coded values as a part of regex.
Notepad ++ solution is: name=".*" lastName=".*"

Does mongo regex query have character limit, if the regex search string is more than that limit it throws error

I am seeing mongo regex query not returning result when the regex searched string is very big, instead its throwing error. I have a scenario where I append lot of names to do a regex and thus my regex search string goes beyond 40000 characters.
eg:
db.getCollection('collection').find(
{"name":{"$regex" :"name1 | name2 | name3", "$options":"-i"}}
)
Can you explain why you are doing this?
The idea of a regex is to create a expression with which you match (multiple) value(s).
example expression:
name\d+
will match on all "namex" vales where x is a decimal.
The idea will be to create a single expression to fullfill your query requirement.
When you want to match on multiple string values you can use $and operator
Yes, mongoDB regex has character limit, originates from perl regex limit.
Because "MongoDB uses Perl compatible regular expressions (i.e. “PCRE” ) version 8.41 with UTF-8 support."MongoDB v3.2
You can see the limitation is 32764 characters:MongoDB add assert of regular expression length
I recently met this issue, and the solution was to use $in query operator instead. $in does not have characters limit. This suits my problem, since it was exact match rather than pattern matching in such long input case.

re2 does not seem to give correct results for some patterns when compared to pcre

I was trying to use both pcre and re2 and I came up with the following observation.
When I give the string as
"ab cd"
and the pattern as
"^[^c]"
re2 returns NO MATCH but its actually a match.
That is to say when I type this RE2::FullMatch("ab cd", RE2("^[^c]")) I get FAIL/No Match.
Please let me know if I am going wrong somewhere or what is the problem?
RE2::FullMatch matches the entire string, like Jerry says.
There are two basic operators: RE2::FullMatch requires the regexp to
match the entire input text, and RE2::PartialMatch looks for a match
for a substring of the input text, returning the leftmost-longest
match in POSIX mode and the same match that Perl would have chosen in
Perl mode.
https://code.google.com/p/re2/wiki/CplusplusAPI

Validate certain string using RegEx

I need to validate following string using Regular Expressions.
These are the constraints which apply for this string.
From beggining it has 9 numbers.
In the end it has character 'V' or 'X'. They can be simple or capital.
Whole length of string must be 10.
Ex: 84256142V, 547812375X
Can anyone provide me RegEx for validate this.
^\d{9}[VX]$ if you put the regex engine in case-insensitive mode, ^\d{9}[vVxX]$ otherwise.
Depends on the language, but it will be something like this:
^[0-9]{9}[VvXx]$
This is the Regex you need: /^(\d){9}(V|X)$/i