Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Coldfusion RegEx

Posted on 2016-10-24
8
Medium Priority
?
113 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 2000 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
Amazon Web Services EC2 Cheat Sheet

AWS EC2 is a core part of AWS’s cloud platform, allowing users to spin up virtual machines for a variety of tasks; however, EC2’s offerings can be overwhelming. Learn the basics with our new AWS cheat sheet – this time on EC2!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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,…
Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
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…

722 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