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)
3 gam (5 beta, 2 alpha)
3 gam (2 alpha, 5 beta)
2 alpha (3 gam, 5 beta)
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
^([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
^([0-9])([ a-z\(]+)([0-9])([ a-z,]+)([0-9])([ a-z\)]+)$
and groups would be:$1, $3, $5
would give you all numbers
ASKER
I need an expression to get the number in front of beta no matter where it is in the string
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Thanks kaufmed!
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