Using Regex to get strings with whitespace within them.

72 次查看(过去 30 天)
Hey guys,
I want to use Regex to acquire some ID's in a cellstring array, the array looks like this:
myString = '(['US04650Y1001', 'US90274P3029', 'US4385161066', 'HON WI', 'US41165F1012', 'US30151E8553', 'US43940T1097', 'US4405431069'])';
My pattern for regex is as follows:
pattern = '[A-Za-z0-9.^_]+';
newArr = regexp(myString, pattern,'match');
I'd like to get the ID called 'HON WI', but with my current pattern, its splitting it into two because of my strings. I would like to get the whole "HON WI", as well as my other strings, everything that's in '', these might have special characters like ^, . or _, but I don't know how to add the whitespace.
Any help is appreciated :)

回答(2 个)

Fangjun Jiang
Fangjun Jiang 2018-11-27
white space in regular expression is \s
  2 个评论
Ajpaezm
Ajpaezm 2018-11-27
编辑:per isakson 2018-11-27
Ok, should I add this to my pattern, like this?
pattern = '[A-Za-z0-9.^_\s]+';
This gives me back a cell array with dimensions of 1x4999, not what I'm looking for.
Fangjun Jiang
Fangjun Jiang 2018-11-28
what is your expected output for this input string? What is your logic?

请先登录,再进行评论。


carlos edurdo condori ochoa
If you only expected one white space in the middle of the strings then this code should work
myString = ['US04650Y1001', "US90274P3029", "US4385161066", "HON WI", "US41165F1012", "US30151E8553", "US43940T1097", "US4405431069"];
regexp(myString,"\w+\^?\.?\,?\s?\w+","match") % We say: It could exist zero or one ocurrence of White space
If there is more than one then use \s*, well all deppends on the text characters that you expect to exist, the ocurrence what goes first or if it could be totally random.

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by