MATLAB Answers

Using Regex to get strings with whitespace within them.

58 ビュー (過去 30 日間)
Ajpaezm
Ajpaezm 2018 年 11 月 27 日
Commented: Fangjun Jiang 2018 年 11 月 28 日
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 :)

  0 件のコメント

サインイン to comment.

件の回答 (1)

Fangjun Jiang
Fangjun Jiang 2018 年 11 月 27 日
white space in regular expression is \s

  2 件のコメント

Ajpaezm
Ajpaezm 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?

サインイン to comment.

サインイン してこの質問に回答します。

製品


リリース

R2016b

Translated by