Skip to content Skip to sidebar Skip to footer

How Do I Combine 2 Regex Patterns Into 1 And Use It Within A Function

I have a regEx for checking a number is less than 15 significant figures, Borrowed from this SO answer /^-?(?=\d{1,15}(?:[.,]0+)?0*$|(?:(?=.{1,16}0*$)(?:\d+[.,]\d+)‌​)).+$/ T

Solution 1:

If you add only one condition of maximum 2 decimal places to first regex, try this..

^-?(?=\d{1,15}(?:[.,]0+)?0*$|(?:(?=[,.\d]{1,16}0*$)(?:\d+[.,]\d{1,2}$))).+$

Demo,,, in which I only changed original \d+ to d{1,2}$

Edited for the reguest to extract 15 significant figures and capture group 1 ($1). Try this which is wrapped to capture group 1 ($1) and limited 15 significant figures to be extracted easily.

^(-?(?=\d{1,15}(?:[.,]0+)?0*$|(?:(?=[,.\d]{1,16}0*$)(?:\d+[.,]\d{1,2}$))).{1,16}).*$

Demo,,, in which changed to .{1,16} from .+$. If the number matches, then able to be replaced $1, but if not so, replaced nothing, thus remains original unmatched number.

Therefore, if you want to extract 15 significant figures by replacing with $1 only when your condition is satisfied, try this regex to your function.

^(-?(?=\d{1,15}(?:[.,]0+)?0*$|(?:(?=[,.\d]{1,16}0*$)(?:\d+[.,]\d{1,2}$))).{1,16}).*$|^.*$

Demo,,, in which all numbers are matched, but only the numbers satisfying your condition are captured to $1 in format of 15 significant figures.


Post a Comment for "How Do I Combine 2 Regex Patterns Into 1 And Use It Within A Function"