2

Given these strings:

var-nvar-ar var-nvar-br var-int-ar var-int-br oth-nvar-ar oth-nvar-br oth-int-ar oth-int-br

I would like to write a regex that selects all the strings that contain my substring if the substring is:

between the beginning of string and -

or

between - and -

or

between - and the end of the string

So if substring = 'var' then the regex would select

var-nvar-ar var-nvar-br var-int-ar var-int-br

I tried:

strings = {'var-nvar-ar' 'var-nvar-br' 'var-int-ar' 'var-int-br' 'oth-nvar-ar' 'oth-nvar-br' 'oth-int-ar' 'oth-int-br'}

regexp(strings,'[^|(*?-)]var[(-.*)|$]','match')

but the result wasn't what I had in mind.

1 Answer 1

1

To use an OR operation (|), a.k.a. alternation, it needs to be inside of a sub-expression () rather than a character set []. Instead of,

regexp(strings,'[^|(*?-)]var[(-.*)|$]','match')

use

regexp(strings,'(^|(*?-))var((-.*)|$)','match','once')
Sign up to request clarification or add additional context in comments.

Comments

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.