0

Let's say I have these strings:

row - 1: First_s - end

row - 2: Second_s - end

...

row - 400: string_400 - end

I'd like to use the matlab function regexp to select any non-white character that is between : and - end. So in my example it would select First_s Second_s string_400, etc. What would an appropriate regex look like?

1
  • Something like this @":(.+?)- end"; Commented Feb 14, 2014 at 21:51

4 Answers 4

2

Instead of lookaheads and lookbehinds, you match the strings that lead and trail the tokens:

>> Cs = regexp(strings,':\ (.*)\ - end','tokens');
>> Cs{1}{1}
ans = 
    'First_s'
>> Cs{2}{1}
ans = 
    'Second_s'
>> Cs{3}{1}
ans = 
    'string_400'
Sign up to request clarification or add additional context in comments.

Comments

1

Using a strict lookbehind and lookahead: (?<=: ).+?(?= - end).

Comments

1

Uses regexprep to replace unwanted parts by nothing:

strings = {
'row - 1: First_s - end';
'row - 2: Second_s - end';
'row - 400: string_400 - end'}; %// example data. Assumed to be a cell array

strings = regexprep(strings,'.*:','') %// remove start
strings = regexprep(strings,'- end$','') %// remove end
strings = regexprep(strings,'\s','') %// remove spaces

The result in this example is:

strings = 

    'First_s'
    'Second_s'
    'string_400'

Comments

0

Here is the regex that works with ruby: row\s\-\s[\d]+\:\s(.*)\s-\send which would probably work with matlab as well.

You can see how this example works here: http://rubular.com/r/FuPwNcBy1I

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.