espresso_coffee
espresso_coffee

Reputation: 6110

Translate regex from JavaScript to ColdFusion?

Hello everyone I have regular expressions that I created in JavaScript and now I would like to have them in ColdFusion. Few problems I run into while trying to put this in ColdFusion, first one was with the blank values. If my variable is empty my regex was outputting false. Second problem is combining these regex in one big line that will check entire row of my text file. For some reason my regex was ignoring upper/lower case. If I have only lower case my regex was outputting true even that is not allowed in this case. Here is my regex:

<cfset fileRegex = "^(?i)^ *[a-z][a-z' .,-]{0,49} *\t(?i) *[a-z][a-z' .,-]{0,49} *\t *(0?[1-9]|1[0-2])\/(0?[1-9]|1\d|2\d|3[01])\/(19|20)\d{2} *\t(?i) *([M|F]) *\t(?i) *(0?[0-9]|1[0-2]|[A-Z]{1,2}) *\t *([^\t].{0,50}) *\t(?i) *([A-Z0-9 ,]{1,102})? *\t *\d{10} *(\t[^\t]*){22}\t *([0-9]{1,4}) *\t([^\t]*)(\t *[A-Z ',.-]{1,50} *)?$">

<cfset myData = "John   Terry   1/29/1981   M   0   London  NULL    0129198109  609 8000    1   England">

<cfif REFind(fileRegex,myData,true) GT 0>
    true<br>
<cfelse>
    false<br>
</cfif>

So in this case last word in my Data set is England and my regex should accept upper case only and it does not, second problem is if I leave this field empty I will get False in return. Also this last word is optional and if I erase that together with the tab I should get true. If anyone can help with this problem please let me know.

Upvotes: 1

Views: 139

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626691

Your regex requires 22 columns with (\t[^\t]*){22}. You may make them optional (-> (\t[^\t]*){0,22}) and the regex will basically work:

(?i)^ *[a-z][a-z' .,-]{0,49} *\t *[a-z][a-z' .,-]{0,49} *\t *(0?[1-9]|1[0-2])\/(0?[1-9]|1\d|2\d|3[01])\/(19|20)\d{2} *\t *([MF]) *\t *(0?[0-9]|1[0-2]|[A-Z]{1,2}) *\t *([^\t].{0,50}) *\t *([A-Z0-9 ,]{1,102})? *\t *\d{10} *(\t[^\t]*){0,22}\t *([0-9]{1,4}) *\t([^\t]*)(\t *[A-Z ',.-]{1,50} *)?$

See the regex demo

Some smaller enhancements:

  • Note you have many (?i) inside, that is not necessary. One single (?i) at the start of the pattern is enough.
  • | inside [M|F] should be removed as it is parsed as a literal pipe symbol. [M|F] matches 1 of 3 characters: either M, |, or F.
  • Please also check if / should really be escaped, I doubt it needs escaping since there are no regex delimiters here.

Upvotes: 2

Related Questions