Regex coding with pattern - regex

I have a long string url, and in it somewhere is "http://www.webcitation.org/65aOmhrdM". I need to write a regex to find "65aOmhrdM" (the pattern is always num,num,lowercase,uppercase,lowercase,lowercase,lowercase,lowercase,uppercase).
I'm looking to say:
var matches = url.match(/regex here/);
Thanks.

Here is a regex for the pattern you gave:
\d{2}[a-z][A-Z][a-z]{4}[A-Z]
If you want to pull it from that url you could do something like this:
/http:\/\/www.webcitation.org\/(\d{2}[a-z][A-Z][a-z]{4}[A-Z])/

\\d{2}[a-z][A-Z][a-z]{4}[A-Z]

Regular is:
\d\d[a-z][A-Z][a-z]{4}[A-Z]

Related

Regex match url pattern

I already copied a regex expression from somewhere like this:
^(\*|http|https|file|ftp|ws|wss|data):\/\/(\*|(?:\*\.)?(?:[^*]+))?\/(.*)$
It can match URL patterns like https://www.google.com/ or https://*.google.com/, I want to support to reach https://www.google.*/, how could I change the regex?
^(\*|http|https|file|ftp|ws|wss|data):\/\/(\*|(?:\*\.)?(\*|(?:[^*]+)))?\/(.*)$

RegEx remove part of string and and replace another part

I have a challenge getting the desired result with RegEx (using C#) and I hope that the community can help.
I have a URL in the following format:
https://somedomain.com/subfolder/category/?abc=text:value&ida=0&idb=1
I want make two modifications, specifically:
1) Remove everything after 'value' e.g. '&ida=0&idb=1'
2) Replace 'category' with e.g. 'newcategory'
So the result is:
https://somedomain.com/subfolder/newcategory/?abc=text:value
I can remove the string from 1) e.g. ^[^&]+ above but I have been unable to figure out how to replace the 'category' substring.
Any help or guidance would be much appreciated.
Thank you in advance.
Use the following:
Find: /(category/.+?value)&.+
Replace: /new$1 or /new\1 depending on your regex flavor
Demo & explanation
Update according to comment.
If the new name is completely_different_name, use the following:
Find: /category(/.+?value)&.+
Replace: /completely_different_name$1
Demo & explanation
You haven't specified language here, I mainly work on python so the solution is in python.
url = re.sub('category','newcategory',re.search('^https.*value', value).group(0))
Explanation
re.sub is used to replace value a with b in c.
re.search is used to match specific patterns in string and store value in the group. so in the above code re.search will store value from "https to value" in group 0.
Using Python and only built-in string methods (there is no need for regular expressions here):
url = r"https://somedomain.com/subfolder/category/?abc=text:value&ida=0&idb=1"
new_url = (url.split('value')[0] + "value").replace("category", 'newcategory')
print(new_url)
Outputs:
https://somedomain.com/subfolder/newcategory/?abc=text:value

Regex Expressions for value extraction from URL param string

I have a string where I'd like to extract some values with regex.
Whole string:
event=B:Rel&time=1511879856&date=20171128-143736&ref=57b3e1ab741d5a017ab8009033350b18&dir=out&src_if=GW1&dst_if=PRI1
I would like to isolate the values between the = and the next & creating this result set for the string given above.
B:Rel
1511879856
20171128-143736
57b3e1ab741d5a017ab8009033350b18
out
GW1
PRI1
Thanks for the help!
Try this:
[^=]+=([^&]+)(?:&|$)
DEMO: https://regex101.com/r/Z3pcR4/1
Depending on your language, you could split the input on this regex:
&?[^=]*=

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

Extract all tokens from string using regex in Scala

I have a string like "httpx://__URL__/__STUFF__?param=value"
This sample is a url by convention...it could be anything with zero or more __X__ tokens in it.
I want to use a regex to extract a list of all the tokens, so output here would be List("__URL__","__STUFF__"). Remember, I don't know beforehand how many (if any) tokens may be in the input string.
I've been struggling but unable to come up with a regex expression that will do the trick.
Something like this did not work:
(?:.?(__[a-zA-Z0-9]+__).?)+
Scala Regex, which is just a wrapper around Java Regex, will never return multiple subgroups for repetitions.
The only way about it is to have a regex for the token, and then find it multiple times. You pretty much already have everything you want:
"__[a-zA-Z0-9]+__".r findAllIn "httpx://__URL__/__STUFF__?param=value"
That returns an Iterator. Use .toSeq or similar to convert into a collection.
Greg, have you tried a simple
_+[^_]+_+
This will match all the __TOKENS__
It doesn't do any check for any __TOKENLIKE__ string after the ?params, but you have mentioned you are not only using that for urls. If you need some refinement, please let us know.
Combine a regex with split:
def urlPathComponents(s: String): Option[Array[String]] =
"""(?<=http(s?)://)[^?]+""".r findFirstIn s map (_.split("/"))