Regex - string optionally ends with asterisk - regex

I need to create a regex to validate urls. Currently the regex I am using allows one or multiple occurrences of /text.
For example:
/text/text
/text
Regex: ^(\/[a-zA-Z\d\-\_]+)+?$
My requirement is I want to use this regex in a validator such that it allows /* in urls but only at the end.
For example:
/text/text/* - Valid
/text/* - Valid
/text/*/text - invalid
Can someone please help me out with this? Thanks

^\/([\w-]*)(\/\1)*((?!\1)[\w-\/])*$
Try this.See demo.
http://regex101.com/r/kP4pZ2/16

Related

regex extract username from 2 types of url

I'm currently using this regex (?<=\/movie\/)[^\/]+, but it only matches the username from the second url, i know i could make a if (contains /movie/): use this regex, else: use another regex on my code, but i'm trying to do this directly on regex.
http://example.com:80/username/token/30000
http://example.com:80/movie/username/token/30000.mp4
To complete the Tensibai's answer, if you have not a port in url, you can use the last dot in url to start your regex :
\.[^\/\.]+\/(?:movie\/)?([^\/]+)
(demo)
You can use something like this to make the movie/ optional and have the username in a named capture group (Live exemple):
\d[/](?:movie\/)?(?<username>[^/]+)[/]
using \d/ to anchor the start of match at after the url.

regular expression - excluding results if string is present

Trying to create a regular expression that excludes results of a substring is present.
Data Set:
http://www.cnn.com/test1
http://www.cnn.com/test3
http://www.cnn.com/test5
http://www.stackflow.com/test4
http://www.cnn.com/test3
http://www.cnn.com/test4
exclude:
find all cnn.com sites
that don't have /test3
Results:
http://www.cnn.com/test1
http://www.cnn.com/test5
http://www.cnn.com/test4
Figured it out: (www.cnn.com)(?!/test3)
If you want to avoid matching strings like http://www.cnn.com/test/test3 then you can use a negtive lookbehind at the end of the string
cnn\.com.*(?<!test3)$
I'm guessing this would be fastest:
cnn\.com(?!\/test3)[a-zA-Z0-9-._~:?##!$&'*+,;=`.\/\(\)\[\]]*
because you restrict the URL to allowed characters only.

Using a wildcard in Regex at the end of a URL in GA

I'm a newbie at Regex. I'm trying to get a report in GA that returns all pages after a certain point in the URL.
For example:
http://www.essentialibiza.com/ibiza-club-tickets/carl-cox/14-June-2016/
I want to see all dates so: http://www.essentialibiza.com/ibiza-club-tickets/carl-cox/*
Here's what I've got so far in my regex:
^https:\/\/www\.essentialibiza\.com\/ibiza-club-tickets\/carl-cox(?=(?:\/.*)?$)
You can try this:
https?:\/\/www\.essentialibiza\.com\/ibiza-club-tickets\/carl-cox[\w/_-]*
GA RE2 regex engine does not allow lookarounds (even lookaheads) in the pattern. You have defined one - (?=(?:\/.*)?$).
If you need all links having www.essentialibiza.com/ibiza-club-tickets/carl-cox/, you can use a simple regex:
www\.essentialibiza\.com/ibiza-club-tickets/carl-cox/
If you want to precise the protocol:
https?://www\.essentialibiza\.com/ibiza-club-tickets/carl-cox(/|$)
The ? will make s optional (1 or 0 occurrences) and (/|$) will allow matching the URL ending with cox (remove this group if you want to match URLs that only have / after cox).

Regex: Get subtext from a string

I have a list of text lines. Each line contains a title and a URL as follows:
product-title-7134 http://domain.com/page-1
another-product-title-822 http://domain.com/page-218
etc.
Using only .NET regex, please help me extract the url from each line.
I understand it can be done by looking at the string from the end until the http is met and output that part but I don't know the exact regex formula for that. Any help is much appreciated.
I would do that with this regex:
http://(\S+)
And find first group in every match.
This regex will math all https:// and http:// links:
(http|https)(://\S+)
You can test this in the .NET regex tester: http://regexstorm.net/tester

regular expression : get super scripted text

I would like to get super scripted text via following html string.
testing to <sup>supers</sup>cript o<sup>n</sup>e
The result I would like to get is like below
supers
n
This is what I tried right now
But the result is not what I want.
<sup>supers
<sup>n
Could anyone give me suggestion please?
You can use lookbehind in your regex:
(?<=<sup>)[^<]*
Update Demo
Use this if there may be other HTML tags between <sup> and </sup>:
(?<=<sup>)(.*?)(?=<\/sup>)
Check the demo.
You were close, just not capturing your match:
Updated regex
(?:<sup>)([^<]*) I just added a capture group around your match
(?<=<sup>)([^<]*?)(?=<\/)
This should work.
See demo.
http://regex101.com/r/sA7pZ0/13