0

I'm learning Regular Expressions.

I'm trying to find the best way of writing an expression that extracts a number from within the parenthesis that follows the character R" so:

Example 1:

String("257*5.6+48.9/2*R(64)")

reg ex would return the numeric value inside R() in this case 64.

Example 2:

String("sin(45)*55 + X^2+R(-64.525)")

reg ex would return the numeric value inside R() in this case -64.525

My expression so far is:

/R\(\-?\d+\.?\d+\)/g

which returns R(number) taking into account possible decimal places and negative values.

Is there a better regular expression to match(return) only the number from inside the parenthesis that follows the character R?

Many thanks in advance for advice / help.

D

3
  • 1
    what language are yo using? Commented Oct 21, 2014 at 8:05
  • Hi, I'm using ActionScript 3. Commented Oct 21, 2014 at 8:07
  • ...extracted from Adobe's website: "ActionScript 3.0 implements regular expressions as defined in the ECMAScript edition 3 language specification (ECMA-262)" Commented Oct 21, 2014 at 8:08

3 Answers 3

2

If you're using a regex engine other than JavaScript, you can use positive lookaround assertions:

/(?<=R\()-?\d+\.?\d*(?=\))/g
Sign up to request clarification or add additional context in comments.

2 Comments

i think you made a mistake by putting \.?\d* in your regex instead of (?:\.\d+)? , i'll delete my answer because it's most similar to your's except the one i mentioned.
@AvinashRaj: You're right, this was just a quick and dirty fix in order not to prevent matches of single-digit integers. But who knows which number formats are valid (.1 and 2. might all be valid, so neither of our regexes might be the ultimate solution)...
2

Get the matched group from index 1

R\((-?\d+(\.\d+)?)\)

Online demo


Use surrounding parenthesis as well for getting the desired output:

R\(([^\)]*)

Online Demo

Pattern explanation:

  R                        'R'
  \(                       '('
  (                        group and capture to \1:
    [^\)]*                   any character except: '\)' (0 or more times)
  )                        end of \1

Try Positive Look behind that do not consume characters in the string, but only assert whether a match is possible or not if supported by the language:

(?<=R\()[^\)]*

Online demo

1 Comment

Hi Braj, in AS3 your second suggestion works (?<=R()[^)]*. For testing RegEx that works in AS3 I've found regexr.com to be more compatible. I don't know enough about RegEx to comment why.
0
R\((\S+?)\)

You can try this.See demo.

http://regex101.com/r/dK1xR4/11

2 Comments

I couldn't get this to return only the number in AS3 although it clearly works in test sites.
@user2190690 try R((.*?))

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.