How to combine a word that is not preceded by "=" using Regex?

I would like to extract characters from Fortran codes in Ruby. Symbols will have the following patterns (NOTE: the type of the variable and parts of the attribute are filtered):

a = b, c(2) ! Match result should be "a" and "c" d(3) = [1,2, & ! Match result should be "d" 3] 

The regex I tried, ((?<!=)\w+(?![^\[]*\]+)(?=( |,|\(|$))) With lookaround stuffs. But because lookbehind restrictions i can't match "= *" to exclude b .

I used Rubular for testing. For your convenience see here .

Thanks in advance!

+4
source share
3 answers

To do regular expression work, you can first replace all trailing spaces after =

 .gsub(/=\s+/, '=').scan(/((?<!=)\w+(?![^\[]*\]+)(?=( |,|\(|$)))/) 
+2
source

One simple thing you could do is split the string into two parts (by '=') and only make the regular expression in the left operand.

This way you do not need to write any complex regular expression.

0
source

My advice would be to split your regular expression into 2 expressions. A regular expression does not always have to be single-line.

0
source

All Articles