Home > Net >  Regex Capture Middle Value
Regex Capture Middle Value

Time:11-24

I would like to ask for your help...

I have this string where I have to get the 4.75. I've tried many regex expression but I could not get it to work and been through browsing lots of examples as well.

Regexr Image

Loan Amount Interest Rate

$336,550 4.75 %

So far, below is my current expression

1. (?<=Interest Rate\s*\n*)([^\s] ). (?=%)

I'm getting the $336,550 4.75

2. ([^\s] ).(?=%)

Resulted into multiple output. In my entire text, which I can't share, there are also other data that is in %.

I am only after the 4.75. I know I can just select the first match via code (i guess) but for now it is not an option.

Thanks in advance!

I've tried different regex expression

CodePudding user response:

You just need to extract "4.75 %" ? Try this:

(?<=Interest Rate\n\n\$\d{3},\d{3}\s)(\d{1,5}\.\d{1,5}\s%)

CodePudding user response:

Since your regex with variable length patterns inside lookbehind works, you can use the following .NET compliant regex:

(?<=Interest Rate\s \S \s )(\S )(?=\s*%)

See the regex demo.

Details:

  • (?<=Interest Rate\s \S \s ) - a positive lookbehind that requires Interest Rate, one or more whitespaces, one or more non-whitespaces and again one or more whitespaces immediately to the left of the current location
  • (\S ) - Group 1: one or more non-whitespace chars
  • (?=\s*%) - a positive lookahead that requires zero or more whitespaces and then a % char immediately to the right of the current location.

CodePudding user response:

Hi Please try this.

[0-9] .[0-9] 

If it does not work let me know which programming language it is.

  • Related