Home Regular expression: Each occurence of word where no special character is preceding
Reply: 0

Regular expression: Each occurence of word where no special character is preceding

user1417
1#
user1417 Published in April 24, 2018, 6:26 am

I really can't get a regular expression to work for the following situation. I'm basically parsing some code files. I want to match each occurence of a word that is not preceded and followed by an alphanumeric character but only if there is no special character preceding this occurence. To make this more clearly an example:

Word to match: test_word

Special character: %

Example text with in square brackets the number of matches I want:

This is some example text                    [0]
(v, c) = test_word                           [1]
test_word(a,b)                               [1]
% a = test_word(b);                          [0]
test_word                                    [1]
a = test_word(d); b = test_word(c);          [2]
a = test_word; % b = test_word;              [1]
d = this_test_word(c)                        [0]

With the simple expression: \Wtest_word\W I get to match each occurence of test_word regardless what comes beforehand. So I tried the expression

(?m)^([^%\n]*?\W)test_word(\W[^\n]*?)$

This works for all but the one case with two occurences. I just cannot get this one to work. I tried with lookaround assertions but I'm really stuck here.

Thank you a lot for your help!

If it matters I'm working in Matlab.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.339182 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO