MATLAB: Regexp Find Digit between two hyphens regular expressions I have a str that looks like this: str = '7-6-1' and I would like to return 6. I was able to return 7 using ^[^-]*[^ -] but can't seem to find the expression to return the second item. Best Answer (?<=-)\d(?=-)Is one of the ways.(?-)\d(?-)might work Related SolutionsMATLAB: Extract the number within the bracket Any number of regular expression would do 'extract whatever is between two brackets:str = 'a_b_c_d(1.0)';regexp(str, '(?<=\()[^)]*(?=\))', 'match', 'once')is one possibility. Thislooks ahead for an opening bracket.matches any number of characters as long as they're not a closing bracketlooks behind for a closing bracket. MATLAB: Extracting consecutive digits using regexp These two returns "123"%%cac = regexp('ITEM 123', '.+?(\d+)', 'tokens' )%%cac = regexp('ITEM 123', '[^\d]+(\d+)', 'tokens' )First, '.+' matches anything up til the end of the text, next it gives back just as little as needed to match '(\d+)' , which is one digit. '.+?' matches as little as needed so that '(\d+)' is able to match the following text. I prefer '[^\d]+(\d+)' Or why not justcac = regexp('ITEM 123', '\d+', 'match' ) Related QuestionHow to use strcmp (or something else) to compare to multiple strings at onceFind strings between using regexpRegular expression for matching substring with wildcardRegexp helpExclude Digits from String using Regexp in MATLABRegexp parentheses string parsing issueRecovering text between parentheses using regexp
Best Answer