Regex - Extract number from a link - regex

I have this link www.xxx.yy/yyy/zzzzzz/xyz-z-yzy-/93797038 and I want to take the number 93797038 in order to pass it into another link.
For example: I want afterwards something like www.m.xxx.yy/93797038 which is the same page as before but in its mobile version.
In general, I know that I have to type www.xxx.yy/(.*) for extracting anything following the in the main url and then I group the result with www.m.xxx.yy/%1 which redirects to the same page but in the mobile version.
Any ideas how to do it?
EDIT: The link www.xxx.yy/yyy/zzzzzz/xyz-z-yzy-/93797038 is automated. The part that is the same each time is only the www.xxx.yy . Every time the system runs produces different urls. I want each time to take the number from those urls, e.g. the 93797038 in my case.

\/(\d+?)$ will get the trailing digits after the final /.

Why you want regex? You can use
string str = #"www.xxx.yy/yyy/zzzzzz/xyz-z-yzy-/93797038";
string digit = str.Split('/').Last();
instead.

Related

How can I get the specific part from the link using regular expressions (regex),,,

The format of Url is - https://example.com/items/html5-templates/654321
Firstly I want to take the link without last numbers.
e.g: https://example.com/items/html5-templates/
And,
Secondly, want to take only the last part (numbers) from the link.
e.g: 654321
I need to get the regex code for using in a auto Parse plugin to my wp site,,,
And then I can customise the link as
Like:
https://.....(link without last part)...../...anything..../...(only last numbers from the link format)...
Thanks a lot for your precious time,,,
use str.replace(/\/\d+$/, '') to remove /86886866 from string like https://www.wexperts.xyz/32423
The result will be like https://www.wexperts.xyz
Try something like this:
^(.*/)(\d+)$
The first part of URL will be in first capture group, and second capture group will be numbers.

Regex to differentiate APIs

I need to create a regex to help determine the number the number of times an API is called. We have multiple APIs and this API is of the following format:
/foo/bar/{barId}/id/{id}
The above endpoint also supports query parameters so the following requests would be valid:
/foo/bar/{barId}/id/{id}?start=0&limit=10
The following requests are also valid:
/foo/bar/{barId}/id/{id}/
/foo/bar/{barId}/id/{id}
We also have the following endpoints:
/foo/bar/{barId}/id/type/
/foo/bar/{barId}/id/name/
/foo/bar/{barId}/id/{id}/price
My current regex to extract calls made only to /foo/bar/{barId}/id/{id} looks something like this:
\/foo\/bar\/(.+)\/id\/(?!type|name)(.+)
But the above regex also includes calls made to /foo/bar/{barId}/id/{id}/price endpoint.
I can check if the string after {id}/ isn't price and exclude calls made to price but it isn't a long term solution since if we add another endpoint we may need to update the regex.
Is there a way to filter calls made only to:
/foo/bar/{barId}/id/{id}
/foo/bar/{barId}/id/{id}/
/foo/bar/{barId}/id/{id}?start=0&limit=10
Such that /foo/bar/{barId}/id/{id}/price isn't also pulled in?
\/foo\/bar\/(.+)\/id\/(?!type|name)(.+)
There is something in your RegEx which is the cause to your problem. "(.+)" RegEx code matches every character after it. So replace it with "[^/]" and add the following code "/?(?!.+)". This is working for me.
/foo/bar/([^/]+)/id/(?!type|name)([^/]+)/?(?!.+)

Changing out one or more pieces of a url in Python 3

I am building image links in Python 3. I am getting a basic starter link and I am being given one or more elements that need to be switched out based on a pattern that is tied to that original link to create my new image link. I am being given the links and the elements and I can tie each link type to a pattern that I can store in the database. What I am looking for is the best way to create the pattern to switch out the data.
For Example:
The first link I get is: https://testurl.com/images/320/new.jpg
I am given a value of 450 and the pattern dictates that the 450 should be swapped out with the 320 to make a new link of: https://testurl.com/images/450/new.jpg.
The second link I get is http://newurl.com/stuff/image.jpg?resize=100
I am given a value of 200 and the pattern dictates that the 200 needs to replace the 100 to make a new link of http://newurl.com/stuff/image.jpg.resize=200
The third link I get is http://anotherlink.com/01/02/2016/today/newfolder/image.jpg I am given a string of 02/15/2016 that needs to be swapped out with the 01/02/2016 and a string of oldfolder that needs to be swapped out with the newfolder giving me a link of http://anotherlink.com/02/15/2016/today/oldfolder/image.jpg
Is there a way to store patterns that could handle all 3 different scenarios? Again, I can have a unique pattern for each, I just would need the same code to be able to execute all 3 patterns successfully.
I was thinking I could do this with a regex and I have done validation with it in the past but I'm not sure if there is a way to actually change out parts of strings based on a pattern instead of just matching.
My other thought was to create a template and do a find and replace loop until all the replace elements have been swapped out but that seems messy. Any suggestions?
I've got this code which should do what you ask, I haven't tested it yet, but add a comment if it doesn't work, or if it doesn't do what you want:
url="" #URL to use
replace="" #string to replace original with
if "resize=" in url:
for i in range(len(url)):
try:
int(url[-1:])
except Exception:
break
else:
url=url[:-1]
url+=str(replace)
elif [e for e in range(len(url)) if url[e:][:(e+10)].replace("/","").isalpha()==False]:
for e in range(len(url)):
if url[:(e+10)][e:].replace("/","").isdigit()==True:
if url[e:][:1]!="/":
url=url[:e]+replace+url[(e+10):]
url=url.replace("newfolder","oldfolder")
break
else:
url1=url.split("/")
url1[len(url1)-2]=replace
url=""
for i in url1:
url+=i+"/"
print(url)
Hope this helps.
EDIT:
I have now tested the code, and fixed the bugs, and it seems to work fine, but still add a comment if it doesn't work.

Using Regex to validate the number of words in a text area

I am attempting to write a MVC model validation that verifies that there is 10 or more words in a string. The string is being populated correctly, so I did not include the HTML. I have done a fair bit of research, and it seems that something along the lines of what I have tries should work, but, for whatever reason, mine always seem to fail. Any ideas as to what I am doing wrong here?
(using System.ComponentModel.DataAnnotations, in a mvc 4 vb.net environment)
Have tried ([\w]+){10,}, ((\\S+)\s?){10,}, [\b]{20,}, [\w+\w?]{10,}, (\b(\w+?)\b){10,}, ([\w]+?\s){10}, ([\w]+?\s){9}[\w], ([\S]+\s){9}[\S], ([a-zA-Z0-9,.'":;$-]+\s+){10,} and several more varaiations on the same basic idea.
<Required(ErrorMessage:="The Description of Operations field is required"), RegularExpression("([\w]+){20,}", ErrorMessage:="ERROZ")>
Public Property DescOfOperations As String = String.Empty
Correct Solution was ([\S]+\s+){9}[\S\s]+
EDIT Moved accepted version to the top, removing unused versions. Unless I am wrong and the whole sequence needs to match, then something like (also accounting for double spaces):
([\S]+\s+){9}[\S\s]+
Or:
([\w]+?\s+){9}[\w]+
Give this a try:
([a-zA-Z0-9,.'":;$-]+\s){10,}

Incremental number in RegEx

I'm using Bulk Image Downloader to download whole images in a forum thread.
But I need the regular expression to identify the page number increments.
The URL string of the page is this:
/topic/2244447/+(page number goes here)
Here's the situation, the page numbers are incremented by +20. So the second page URL is /topic/2244447/+20 and third page is /topic/2244447/+40 and so on.
How can I put the regex for this?
\/topic\/2244447\/\+([0-9]*[02468])?0$
Just being careful:
I took a look at the documentation, page 28 of the Bulk Image Downloader user's guide.
I wonder whether your page numbers end with 0 or 1.
If the + will not exist anywhere else, then this should work although it will match any number with any number of digits after the +.
\+\d+