craig
craig

Reputation: 26262

Regex named-capture groups in T-SQL

I need to extract ICD 9 values from a requirements document. The ICD 9 values could be individual codes like V91.19 or ranges 441.00-441. For example:

    4. Peripheral vascular disorders - ICD-9-CM codes: 440.0-440.9, 441.00-441.9, 442.0-442.9, 443.1-443.9, 447.1, 557.1, 557.9, V43.4, V91.19, V9000, V91, M8440/0

Ultimately, the goal is to use these values in a WHERE clause:

SELECT * 
FROM ICD9 
WHERE (
        (CODE BETWEEN '440.0' AND '440.9')
        OR (CODE BETWEEN '441.00' AND '441.9')
        ...
        OR CODE IN ('447.1', '557.1', '557.9', 'V43.4', 'V91.19', 'V9000', 'V91', 'M8440/0') 
      )

This regex:

/[A-Z]?[0-9]+[\.\/]?[0-9]*/g

matches:

How do I need to modify my regex to:

Upvotes: 0

Views: 823

Answers (1)

Dalorzo
Dalorzo

Reputation: 20014

Do you mean like this?

[A-Z]?[0-9]+[\.\/]?(?=\d)[0-9]*

where

  • (?=\d) The positive lookahead - Assert that the regex can be matched only if a digit [0-9]

Same results is if your remove the optional * part of the last digit like and replace it with +:

[A-Z]?[0-9]+[\.\/]?[0-9]+

https://regex101.com/r/nK3zB3/2

About the ranges and groups I think it could something like:

(([A-Z]?[0-9]+[\.\/]?[0-9]+)[-]*(([A-Z]?[0-9]+[\.\/]?[0-9]+))?)

Online Demo

Upvotes: 1

Related Questions