-3

I am trying to use Regex to match a word within a www URL. In the event that the www does not exist, I would still like it to match on the '.com'. For example if the following text was present...

www.anything.com

anything.com

It would return "anything" as the result. In the event that the 'www' is not present, it could still get it from the word before '.com'. But if the 'www' is present, I would like it to look there first.

Any thoughts?

JadonR
  • 143
  • 2
  • 8

1 Answers1

0

You can use Perl:

$ echo "www.anything.com
> anything.com" | perl -lane 'print $1 if /^(?:www\.)?([^.]+)\.com$/'
anything
anything
dawg
  • 80,841
  • 17
  • 117
  • 187
  • Thanks. However Perl is not compatible with my software that uses the Regex statements. – JadonR Sep 01 '18 at 17:52
  • Specify how / where you are trying to use a regex. – dawg Sep 01 '18 at 17:54
  • False positives include `C:\Windows\System32\diskcopy.com`. On the other hand, `meta.stackoverflow.com` will not be matched. – 41686d6564 Sep 01 '18 at 17:57
  • Neither of those were specified as possible input strings. The user specifically states *In the event that the 'www' is not present, it could still get it from the word before '.com'* so by that criteria `C:\Windows\System32\diskcopy.com` should match and the user stated only `www` as the only lead clause, so `meta.stackoverflow.com` should not match. – dawg Sep 01 '18 at 18:05