Search code examples
regexcoldfusion

How can I convert a working regular expression in regex101 to Coldfusion


[https://regex101.com/r/qNDKIh/1/][1]

How can I convert a working regular expression in regex101 to Coldfusion REMatchNoCase [1]: https://regex101.com/r/qNDKIh/1/

Coldfusion Code

<cfset VinDetail = ReMatchNoCase("^([A-HJ-NPR-Z0-9]{3})([A-HJ-NPR-Z0-9]{5})([0-9X])([A-HJ-NPR-Z0-9])([A-HJ-NPR-Z0-9])([A-HJ-NPR-Z0-9]{6})$", "1G1PA5SH9D7126083") />

<cfdump var="#VinDetail#">

Getting Malformed regular expression error

Regex101 output

Full match  0-17    1G1PA5SH9D7126083
Group `wmi` 0-3 1G1
Group `vds` 3-8 PA5SH
Group `check`   8-9 9
Group `vis` 9-17    D7126083
Group `year`    9-10    D
Group `plant`   10-11   7
Group `seq` 11-17   126083

Solution

  • The single result matching the input string is 'correct' - because ReMatch() only returns matches, and that is the actual match. If you want to return the capture groups then you can use ReFind() with returnsubexpressions=true, eg:

    <cfset VinDetail2 = ReFindNoCase("^([A-HJ-NPR-Z0-9]{3})([A-HJ-NPR-Z0-9]{5})([0-9X])(([A-HJ-NPR-Z0-9])([A-HJ-NPR-Z0-9])([A-HJ-NPR-Z0-9]{6}))$", "1G1PA5SH9D7126083", 1,true,'ALL') />
    <cfdump var="#VinDetail2[1].MATCH#" />
    <cfdump var="#VinDetail2#" />
    

    ReFindNoCase()-full-result

    https://cffiddle.org/app/file?filepath=e4d3f15a-bd35-4a70-b806-3eec7a1fdf47/a8e67f20-a3ec-4d8d-9c0b-d4108a6aaea2/a2d7b829-36ba-4f55-9271-4aec369143cb.cfm