0

I've looked through Regex Last occurrence? but cannot get the regex to work for my example string ("https://www.fakesite.com test one"). I need to return the last character of the website name only (or the position). I have the expressions for both capturing the site and obtaining the last character but cannot get the expression to get the right look behind.

(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s] 
{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.| 
(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,}) <- Regular Expression for website

(?=.?$).   <-  Regular Expression for retrieving last character

I've been using https://regex101.com/ to try and find, but no luck.

How can I retrieve the last character or position?

LastCharacterImage

--Edit--

How can I retrieve just the last character of any string? (I need just the letter 'r' in System Engineer). 'System Engineer' is dynamic.

"for the position of System Engineer located in"

(?<=position of )(.*)(?= located)   <- regex to capture System Engineer between words 'position of' and 'located'
William Humphries
  • 541
  • 1
  • 10
  • 21
  • 1
    the big regex hasd probleems. the last char regexr is `[\S\s]$` –  Jun 15 '20 at 19:48
  • Yes, but if I didn't know the string and need to capture that first how can I achieve that (I didn't know the text would be 'System Engineer') – William Humphries Jun 17 '20 at 12:45
  • 1
    You need to know something about the String or pattern, because that's the way regular expressions work behind the scene. I can give you a work around in the programming language though. Let's say you get some dynamic string as input like **System Engineer** for example; then what you can do is you can create the regex dynamically. Something like `String str = "SomeString"; String regex = "(?<=" + str + ")\w;` –  Jun 17 '20 at 12:51
  • Yes, I know the text will be between the words "position of" and "located" – William Humphries Jun 17 '20 at 12:53
  • 1
    Please check [**this**](https://regex101.com/r/oTA5eS/2). Group 1 does it for you. Does it help? –  Jun 17 '20 at 12:57
  • Yes, that will work, thank you :) – William Humphries Jun 17 '20 at 13:12

1 Answers1

1

You may try the below regex. The below regex will check for valid url address as well as will get you the last character of your url.

https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}(\w)\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*)

Explanation of the above regex:

https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256} - Matches the http/https:// part of the regex along with www and the domain name before the first ..

[a-zA-Z0-9()]{1,6} - This part matches the last of the url part.

(\w) - Represents a capturing group capturing the last character of the url. You may use ([a-zA-Z0-9]) manually if you don't want to include _.

\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*) - Matches the rest part of the url like .uk or .in, etc. zero or more times.

You can find the demo of the above regex in here.

Reference: The regex for matching the valid url is taken from this answer.

If you want to get amendments in your regex; just add [a-zA-Z] after your regex. You can find the demo here.

Community
  • 1
  • 1
  • Can you help with the edit I made, I need help making this generalized, I read through the Explanation but could not get the last character of the string and capture the second group. – William Humphries Jun 17 '20 at 12:37