Link to home
Start Free TrialLog in
Avatar of NewtonianB
NewtonianB

asked on

regex find number before word

How can I write a regex to match the number for a specific word i provide such as alpha, beta, gam

3 gam (5 beta, 2 alpha)
3 gam (2 alpha, 5 beta)
2 alpha (3 gam, 5 beta)
Avatar of jeromee
jeromee
Flag of United States of America image

Hi NewtonianB,
I'm not sure I understand your request but let me take a shot at it...

% cat /tmp/ee1
3 gam (5 beta, 2 alpha)
3 gam (2 alpha, 5 beta)
2 alpha (3 gam, 5 beta)
%  perl -ne'$type="alpha"; ($data)=/(\d+)\s+$type/; print "$data\n"' /tmp/ee1
2
2
2


You could group:

^([0-9])([ a-z\(]+)([0-9])([ a-z]+)([0-9])([ a-z\)]+)$

and groups would be:$1, $3, $5
would give you all numbers
You could group:

^([0-9])([ a-z\(]+)([0-9])([ a-z,]+)([0-9])([ a-z\)]+)$

and groups would be:$1, $3, $5
would give you all numbers
Avatar of NewtonianB
NewtonianB

ASKER

I need an expression to get the number in front of beta no matter where it is in the string
ASKER CERTIFIED SOLUTION
Avatar of jeromee
jeromee
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kaufmed
__NO POINTS__

I agree with jeromee  ( http:35235729 ).
Thanks kaufmed!