Regex to find 3 forward and backword from numbers

Hi Team,
I need find all numbers from below paragraph and once you find that numbers then we need to find 3 forward and backward words from that number.
Paragraph is below

Key Rating Drivers Higher LTV than Recent Freddie Mac Transactions: The pool’s Fitch-stressed LTV of 122.3% is higher than both the 2019 and 2018 Fitch-rated Freddie Mac 10-year K-series transactions averages of 120.0% and 119.3%, respectively. The pool’s Fitch-stressed DSCR of 1.01x is similar to that of both the 2019 and 2018 Fitch-rated Freddie Mac 10-year K-series transactions averages of 1.01x and 1.00x, respectively. Limited Amortization: The pool is scheduled to amortize by 7.5% of the initial pool balance prior to maturity, which is below the 2019 and 2018 Fitch-rated Freddie Mac 10-year averages of 8.0% and 7.8%, respectively. Twelve loans (30.5% of the pool) in the pool are fullterm interest-only loans, 33 loans (63.3% of the pool) are partial interest-only loans, and the remaining seven loans (6.3% of the pool) provide for amortization through the term of the related underlying mortgage loan. None of the underlying mortgage loans fully amortize over their term. Limited Non-Traditional Multifamily Exposure: The pool is secured by 98.2% traditional multifamily properties, 1.3% by senior housing (independent living) and 0.5% by manufactured housing communities (MHCs). No loans are secured by either traditional healthcare or exclusively student housing properties. The pool’s traditional multifamily concentration is above the 2019 and 2018 Fitch-rated 10-year averages of 95.6% and 94.2%, respectively. Healthcare properties have a higher probability of default in Fitch’s multiborrower model than traditional multifamily property types. Student housing properties are the biggest contributor to overall CMBS multifamily defaults.


I don’t think this can be done with a regex with your example. The problem comes in where you have less than 3 words that do not contain a digit before or after a digit. Regexs read data going forward, and once a part of the string has been passed, it cannot be reread in the same regex.

However, to get the values, you can use the Split method to split this string at the spaces. Then you create a list of indices on your array created from Split where you find a match with regex .*\d+.*. Then you know that the 3 words preceding the number are in indices ix -1, ix-2, and ix-3, where ix is an index where a number is found. Just watch out for boundary conditions, where the index cannot be less than 0 or greater than the length of the array - 1.

1 Like