Solved

Coldfusion RegEx

Posted on 2016-10-24
8
44 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
Comment Utility
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
Comment Utility
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 24

Expert Comment

by:dgrafx
Comment Utility
<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
 

Author Closing Comment

by:nachtmsk
Comment Utility
Worked great. Thanks.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 24

Expert Comment

by:dgrafx
Comment Utility
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
Comment Utility
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 24

Expert Comment

by:dgrafx
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Parse CSS value with RegEx 2 39
Align a <div> to a table row 3 38
regex to extract ip:john 17 55
awk problem filtering combo 12 18
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now