Thoth
Thoth

Reputation: 1041

Split alternating string number appearence

Sorry for my question, I am new in regular expressions, I am looking a clever way to split strings of this pattern A007B001C017D021E041 into A, 007, B, 001, C, 017, D, 021, E, 041. In other words, the input is a [string][threeDigits] pattern repeated five times and the output is a separation [string],[threeDigits] for every repetition. Could you please give a suggestions using regular expressions of a matlab build-in function?

Thanks.

Upvotes: 0

Views: 47

Answers (2)

yaccob
yaccob

Reputation: 1333

A python-based regex solution would look like this - no idea if matlab supports the same regex syntax:

$ echo A007B001C017D021E041 | sub '([A-Z])+([0-9]+)' '\1, \2, ' 
A, 007, B, 001, C, 017, D, 021, E, 041, 

I defined sub as an alias like this:

alias sub='python -c "import sys, re; regex=re.compile(sys.argv[1]); [ sys.stdout.write(regex.sub(sys.argv[2], line)) for line in sys.stdin ]"'

Upvotes: 1

DVarga
DVarga

Reputation: 21799

A possible solution:

tokens = regexp(str, '([a-zA-z])(\d\d\d)', 'tokens');
array = [tokens{:}];

Upvotes: 1

Related Questions