Solved

Coldfusion RegEx

Posted on 2016-10-24
8
63 Views
Last Modified: 2016-11-02
Hi,
I'm using CF 11.
I need to pull the URL that the page is on and see if there is a subdomain present. If so, I need to store that subdomain name, in a variable. I could easily do this in Perl or Python, but I have no idea how to do it in CF.

To be clear:
<cfset currentURL = "#CGI.SERVER_NAME#">
<cfoutput>#currentURL#</cfoutput>
Let's say 'currentURL' has   paul.beatles.com in it.
And I want to store 'paul' in a var to later check and see if 'paul' matches a particular string I'm looking for.

How would I do that in CF?  Mostly I need to know how to pull 'paul' out of paul.beatles.com and store 'paul' in a var.

Thanks!
Nacht
0
Comment
Question by:nachtmsk
  • 4
  • 3
8 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 500 total points
ID: 41857951
Perhaps something like this? What kind of values do you get in the currentURL variable?
<cfset subdomain = REReplace( currentURL, "\.[^.]+\.com$",  "" ) />

Open in new window

1
 

Author Comment

by:nachtmsk
ID: 41858575
Hi Terry,
Thanks for the reply.

The currentURL var will either have something like  www.mydomain.com or  someothername.mydomain.com
I will need to ignore  the 'www' subdomain, but save the 'someothername' subdomain in another var.
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 41858814
<cfscript>
subdomain="";
if(listlen(cgi.server_name,".") > 2 && !isNumeric(replace(cgi.server_name,".","","all")) && listfirst(cgi.server_name,".") != "www") {
      subdomain=listfirst(cgi.server_name,".");
}
writeoutput(subdomain);
</cfscript>
1
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Closing Comment

by:nachtmsk
ID: 41868384
Worked great. Thanks.
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 41868460
The answer you accepted won't do what you asked for.
Did you accept the wrong answer?
Could you explain please ...
0
 

Author Comment

by:nachtmsk
ID: 41869388
dgrafx --
it did do what I asked. It pulled the subdomain out and stored it in a variable.
His answer was also simple and easy to follow.
I appreciated your input, but your answer was overly complicated for my needs.
0
 
LVL 25

Expert Comment

by:dgrafx
ID: 41869500
Thanks for responding = however the solution you accepted will not do as you asked even though as you say it is simpler.
Remember asking for an exception for the "www" subdomain?

The solution accepted will return the following:
www.domain.com = www
domain.com = domain.com
xyz.domain.net = xyz.domain.net

and so on and so forth
the only situation it works in is subdomain.domain.com
it doesn't function correctly at www.domain.com or if a user navigates to domain.com or if you are at a .net or .io or any other tld besides .com

Thanks for listening & good luck ...
0
 

Author Comment

by:nachtmsk
ID: 41870258
Hi Again Dgraf --

I appreciate your being so thorough.  
I can ignore www if returned. Also, it's fine that it only works for .com. This is only needed for one domain name, which has the potential for many subdomains and it works perfectly for the isolated case in which I need it. I could have been more precise in my original post through, your correct. I'm sorry I wasn't.

Thanks again for your thoroughness !
I'm sure I will post more questions in the future on CF, as it's not something I have a lot of experience in.
nacht
1

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Regular expression to allow alpha numeric plus some other characters. 12 48
Align a <div> to a table row 3 77
Bulk Reorder File Names 4 67
regx  exclude  pattern 6 43
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question