0

I have a follow up question to this post: RegEx to exclude match if a certain word is present, but not another partial word .

If given the following string.

x*vec*grad(vec)

How do I find 'vec' but not 'grad(vec)'?

I am assuming I will need to use something like the following for the search expression, but this doesn't work for me.

vec(?!(?<=grad\(vec))

I also want to ignore the space inside the parentheses, i.e. it should work for the following.

x*vec*grad( vec )

I am using MATLAB's regexp function. Thanks.

1 Answer 1

2

This works in c#, it should port to MATLAB easily enough:

(?<!grad\()vec

It matches vec*grad but not x*grad(vec).

The trick is to use the negative lookbehind (<!) instead of tyring to invert the positive lookbehind (<=).

Sign up to request clarification or add additional context in comments.

2 Comments

Adding the \s* accounts for whitespace. So, (?<!grad\(\s*)vec is doing what I need. Thanks.
Well, it will break with vec*somefunc(vec), or funky stuff like vec*outerfunc(innerfunc(vec) * vec * factor, vec, somethingelse)

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.