1

Hello where is some text patterns like:

some text Here +0.25(2)
some text Here 0.25(2.3)
some text Here 0.00(2.3)
some text Here -1.5(1.5)
...
some text Here param_1(param_2)

I need to extract two values param_1 and param_2. How to solve it using regexpressions? (needed Javascript)

param_1 is number contais +, - or nothing perfix. param_2 is number

1
  • Do you want every single param_1 and param_2 in their own array? So, for example, above code would output: [0.25, 0.25, 0.00, -1.5] and [2, 2.3, 2.3, 1.5]. Commented Feb 1, 2015 at 18:21

2 Answers 2

1
([+-]?\d+(?:\.\d+)?)\((\d+(?:\.\d+)?)(?=\))

Try this.See demo.Grab the captures.

https://regex101.com/r/vD5iH9/25

var re = /([+-]?\d+(?:\.\d+)?)\(\d+(?:\.\d+)?(?=\))/g;
var str = 'some text Here +0.25(2)\nsome text Here 0.25(2.3)\nsome text Here 0.00(2.3)\nsome text Here -1.5(1.5)\n...\nsome text Here param_1(param_2)';
var m;

while ((m = re.exec(str)) != null) {
if (m.index === re.lastIndex) {
re.lastIndex++;
}
// View your result using the m-variable.
// eg m[0] etc.
}

EDIT:

(.*?)\s([+-]?\d+(?:\.\d+)?)\((\d+(?:\.\d+)?)(?=\))

use this to capture all three components.See demo.

https://regex101.com/r/vD5iH9/28

Sign up to request clarification or add additional context in comments.

3 Comments

thanks! Another one, how to get some text Here using expressions?
In the provided demonstration it appears that the parenthesis are captured as well - surely this would be unwanted behaviour?
@minseong you need to grab tha cpature.edited with final answer as well
1

I assume that you would like an array of param_1s, and then an array of param_2s.

You can accomplish this with two simple regex's:

(to capture param_1s):

/[+-\d.]+(?=\([\d.]+\)$)/gm

param_1 demo

and param_2's are even simpler:

/[\d.]+(?=\)$)/gm

Try the full jsFiddle demo.

var param1 = str.match(/[+-\d.]+(?=\([\d.]+\)$)/gm);
var param2 = str.match(/[\d.]+(?=\)$)/gm);

param1 is now an array containing param_1s, and param2 for param_2s.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.