-1

I have the following string:

https://www.google.com/today/sunday/abcde2.hopeho.3345GETD?weatherType=RAOM&...
https://www.google.com/today/monday/jbkwe3.ho4eho.8495GETD?weatherType=WHTDSG&...

I'd like to extract jbkwe3.ho4eho.8495GETD or abcde2.hopeho.3345GETD. Anything between the {weekday}/ and the ?weatherType=.

I've tried (?<=sunday\/)$.*?(?=\?weatherType=) but it only works for the first line and I want to make it applicable to all strings regardless the value of {weekday}.

I tried (?<=\/.*\/)$.*?(?=\?weatherType=) but it didn't work. Could anyone familiar with Regex can lend some help? Thank you!

[Update] I'm new to regex but I was experimenting it on sublime text editor via the "find" functionality which I think should be PCRE (according to this post)

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
noobie2023
  • 721
  • 8
  • 25

2 Answers2

3

Try this regex:

(?:sun|mon|tues|wednes|thurs|fri|satur)day\/\K[^?]+(?=\?weatherType)

Click for Demo

Link to Code


Explanation:

  • (?:sun|mon|tues|wednes|thurs|fri|satur)day - matches the day of a week i.e, sunday,monday,tuesday,wednesday,thursday,friday,saturday
  • \/ - matches /
  • \K - unmatches whatever has been matched so far and pretends that the match starts from the current position. This can be used for the PCRE.
  • [^?]+ - matches 1 or more occurences of any character that is not a ?
  • (?=\?weatherType) - the above subpattern[^?]+ will match all characters that are not ? until it reaches a position which is immediately followed by a ? followed by weatherType

To make the match case-insensitive, you can prepend the regex with (?i) as shown here

Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43
1

In the examples given, you actually only need to grab the characters between the last forward slash ("/") and the first question mark ("?").

You didn't mention what flavor regex (ie, PCRE, grep, Oracle, etc) you're using, and the actual syntax will vary depending on this, but in general, something like the following (Perl) replacement regex would handle the examples given:

s/.*\/([^?]*)\?.*/$1/gm

There are other (and more efficient) ways, but this will do the job.

Mike Fahy
  • 5,487
  • 4
  • 24
  • 28