ColdFusion regular expression excluding the word

I'm having trouble creating a regex using ColdFusion 10. I need reFind () to return zero if the url contains "dev" at the end of any subdomain with "mydomain.com" in it.

reFind(THE_REGEX, "subdomaindev.mydomain.com") needs to return 0 reFind(THE_REGEX, "subdomain.mydomain.com") needs to return a positive number 

I found the following in the Adobe documentation: ( http://help.adobe.com/en_US/ColdFusion/10.0/Developing/WSc3ff6d0ea77859461172e0811cbec0a38f-7ffb.html ) and based on this I tried to use the lookahead concept.

Thought this would work, but it’s not:

  reFind("(?!dev)\.mydomain\.com$", "subdomaindev.mydomain.com") = 13 reFind("(?!dev)\.mydomain\.com$", "subdomain.mydomain.com") = 10 

I don't understand why this gives zero for both:

  reFind("(?=dev)\.mydomain\.com$", "subdomaindev.mydomain.com") = 0 reFind("(?=dev)\.mydomain\.com$", "subdomain.mydomain.com") = 0 

These are the results I expected from (? =):

  reFind("(?:dev)\.mydomain\.com$", "subdomaindev.mydomain.com") = 10 reFind("(?:dev)\.mydomain\.com$", "subdomain.mydomain.com") = 0 

NOTE. This is intended for use with the ColdBox environment configuration, where I can only pass one regular expression of the environment variable, which then calls the method for a consistent environment. I would prefer not to have a second check in this method to find β€œdev.”, But if I do.

Thank you for your help!

+4
source share
2 answers

(too long for comments)

I don't understand why this gives zero for both

reFind("(?=dev)\.mydomain\.com$", "subdomaindev.mydomain.com") = 0

Verily, me too. However, I came across this topic , which offers a plausible explanation. Rephrase (using your values):

Looks ahead from the face on which they are located, and you placed it in front . . So, what you have is actually saying "everything that ends on .mydomain.com if the first three characters starting at this position ( .my ) are not dev ", which is always true.

.. or in the case of (?=dev) always always false, because obviously .my characters .my never be equal to dev .

Further search opened a detailed blog entry by Adam Cameron on regular expressions and browsing . The Negative Views section contains an example expression used to validate a string that does not contain the word CAT:

 ^(?!.*CAT).*$ 

A blog entry provides a better explanation, but mostly uses ^ (start), $ (end) and .* (Zero or more characters) to search the entire string. While your current expression only searches for characters following it, that is, ".mydomain.com".

If I understand correctly, you can use the approach described above to confirm that the supplied line does not end with "dev.mydomain.com". Just change "CAT" to the substring you are trying to match ... err ... does not match. Not very verified, but something like that:

 reFind("^(?!.*dev\.mydomain\.com$).*$","subdomain.mydomain.com") reFind("^(?!.*dev\.mydomain\.com$).*$","subdomaindev.mydomain.com") 

Results:

  • 0 ==> "subdomaindev.mydomain.com"
  • 1 ==> "subdomain.mydomain.com"

Disclaimer: I am not an expert on regular expression, fantasy in any way , so it’s quite possible that there are better options. However, I hope this helps explain why the current expression does not work as you expected.


Update:

As noted in @zabuuq's comment, the final working expression:

 ^(?!.*dev\.mydomain\.com).*\.mydomain\.com$ 
+1
source

Regular expressions are not so useful when NOT trying to find content.

The following may give you a start in the right direction:

 ^((?!dev).)*$ 

The above will return zero if (dev) is anywhere in the domain. You may need to play with this to apply it only to part of the subdomain.

If you can add your own code (so you don't need to use a regex), you can do something like this. (This will probably also be easier for someone to understand by regular expression):

 <cfset fqdn = "subdomaindev.mydomain.com"> <cfset subdomain = getToken(fqdn,1,".")> <cfset isDev = (len(subdomain GTE 3) AND right(subdomain,3) EQ "dev")> <cfoutput> #isDev# </cfoutput> 
0
source

All Articles