Solved

Coldfusion RegEx

Posted on 2016-10-24
8
100 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 
LVL 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
Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

 
LVL 1

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
 
LVL 1

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
 
LVL 1

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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

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…
PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
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…
Suggested Courses

623 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