Regex for url route with query string - regex

I am having hard time learning regex and honestly I have no time at the moment.
I am looking for a regex expression that would match url route with query string
What I need is regex to match population?filter=nation of course where nation can be any string.
Based on my current regex knowledge I have also tried with regex expression /^population\/(?P<filterval>\d+)\/filter$/ to match population/nation/filter but this does not work.
Any suggestion and help is welcome.

This does match only your first query string format:
population\?filter=[\w]+[-_]?[\w]+
Addiotionally it allows for - and _ as bindings between words. If you know, that your string ends right there, you can also add an $ to the end to mark it so.
If you know that the nation is only alphabetical characters, yu can use the simplified version:
population\?filter=[\w]+
Demo

Related

Split complex string into mutliple parts using regex

I've tried a lot to split this string into something i can work with, however my experience isn't enough to reach the goal. Tried first 3 pages on google, which helped but still didn't give me an idea how to properly do this:
I have a string which looks like this:
My Dogs,213,220#Gallery,635,210#Screenshot,219,530#Good Morning,412,408#
The result should be:
MyDogs
213,229
Gallery
635,210
Screenshot
219,530
Good Morning
412,408
Anyone have an idea how to use regex to split the string like shown above?
Given the shared patterns, it seems you're looking for a regex like the following:
[A-Za-z ]+|\d+,\d+
It matches two patterns:
[A-Za-z ]+: any combination of letters and spaces
\d+,\d+: any combination of digits + a comma + any combination of digits
Check the demo here.
If you want a more strict regex, you can include the previous pattern between a lookbehind and a lookahead, so that you're sure that every match is preceeded by either a comma, a # or a start/end of string character.
(?<=^|,|#)([A-Za-z ]+|\d+,\d+)(?=,|#|$)
Check the demo here.

URL regex that skips ending periods

I'm trying to create a regex that matches url strings within normal text. I have this:
http[s]?://[^\s]+
This seems to work well with the exception that if the url is at the end of a sentence it will grab the period as well. For example for this string:
I am typing some text with the url http://something.com/something-?args=someargs. This is another sentence.
it matches:
http://something.com/some-thing?args=someargs.
I would like it to match:
http://something.com/some-thing?args=someargs
Obviously I can't exclude periods because they are in the url previously but I can't figure out how to tell it to exclude the last period if there is one. I could potentially use a negative lookahead for end of line or whitespace, but if it's in the middle of the line (without a period after it) that would leave off the last character of the url.
Most of the ones I have seen online have the same issue that they match the ending dot so maybe it's not possible? I know basic regex but certainly not a genius with it so if someone has a solution I would be very grateful :).
Also, I can do some post-process in this case to remove the dot if I need to, just seems like there should be a Regex solution...
Try this one
http[s]?://[^\s]+[^. ]

Pattern Matching Multiple Word Patterns

I'm hoping someone can help with some regular expression. I'm trying find instances where two patterns exist in a string (I think I'm saying that right).
Here is my test string:
{"eventid": 2121, "username":"FRED", "starttime": "1550243080", "newprocessname": "C:\\Windows\\System32\\wbem\\WmiPrvSE.exe", "parentprocessname": "C:\\Windows\\System32\\svchost.exe"}
I want to be able to search based on one or more criteria. The problem I have is when multiple criteria is provided. For example, the following seems to match if username is good or newprocessname is good whereas I want it to match only if both are good.
("username"\s*:\s*"(.*?)FRED(.*?)")|("newprocessname"\s*:\s*"(.*?)WINDOWS(.*?)")
I think my patterns are right, but how do I return a result only if both patterns match?
I hope I'm explaining this correct???
Thank you!
Jon
If you're using a regex library that supports lookaheads you can use this regex:
^(?=.*"username"\s*:\s*"([^"]*)FRED([^"]*)")(?=.*"newprocessname"\s*:\s*"([^"]*)WINDOWS([^"]*)")
It looks for both the username and newprocessname tags at the same time (as the lookaheads don't consume any characters). Note that you need to change .*? in your regex to [^"]* otherwise you can match text further into the string than the actual value associated with the tag name.
Demo on regex101

What's the right regular expression to match the exact word at the end of a string and excluding all other urls with more chars at the end?

I have to match an exact string at the end of a url, but not match all other urls that have more characters after that string
I can better explain with example.
I need to match the url having the string 'white' at its end: http//mysite.com/white
But I also need to not match urls having one or more characters postponed to it, like http//mysite.com/white__blue or http//mysite.com/white/yellow or http//mysite.com/white/
How to do that?
Thanks
Regex to match any url*
^(https?:\/\/)?([\da-z\.-]+\.[a-z\.]{2,6}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?$
Regex to match a url containing white in the end
^(https?:\/\/)?([\da-z\.-]+\.[a-z\.]{2,6}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?white$
You can check the regex here
From regexr.com
It does not match urls(which are not valid anyway) like
httpabrakadabra.co//
http:google.com
http://no-tld-here-folks.a
http://potato.54.211.192.240/
Based on your limited sample inputs, I'd say you could get away with this very minimal pattern:
^http[^\s]+white$
However, depending on what you are truly trying to achieve, what language/function you are implementing this pattern with, and what the full input string looks like, this pattern may need to be refined.
It would be best if you would improve your question to include all of the above relevant information.

Regex substring

I'm trying to select a substring using regex and I'm going round in circles. I need to select everything before the first "_".
exampale URL - GI_2013_JUNE_10_VOL3_LASTCHANCE
So the result Im looking for from the URL above would be "GI". The text before the first "_" can vary in length.
Any help would be much apprecited
The regex would be:
^[^_]+
and grab the whole regex match. But as a comment says, using a substring function is more efficient!
^[^_]*
...is the expression you're looking for.
It basically says: Select everything that is not an underscore, starting at the beginning of the string.
http://regexr.com?356in