String Search with Special Characters

조회 수: 7 (최근 30일)
dsmalenb
dsmalenb 2019년 5월 27일
편집: Stephen23 2019년 5월 28일
Team,
I have reviewed quite a few posts on this subject but I must be missing something since I cannot get my string search working. If you can help I would be very thankful. Say we have the following:
Say I have a string that is built up as follows:
  1. 1st character is >
  2. 2nd-5th characters are ABCD
  3. 6th character is a space
  4. There are a variable number of alphanumerical characters (i.e. 0 through 9, a-z, A-Z) for the 7th slot etc.
  5. #4 is followed by a period
  6. There are again a variable number of alphanumerical characters (i.e. 0 through 9, a-z, A-Z)
  7. The last character is a <
How would you set up the regexp syntax to dertermine the locations in your string where these cases happen?
Here are two sample strings:
S = '>ABCD 1010.01<';
S = '>ABCD 2345678.123<';
Thank you for your help!

채택된 답변

Raouf Amara
Raouf Amara 2019년 5월 27일
pat = '>ABCD \w*.\w*<';
str = 'whateveryouwant>ABCD 2345678.123<andmore>ABCD 1010.01<';
idx = regexp(str,pat)

추가 답변 (1개)

Stephen23
Stephen23 2019년 5월 27일
편집: Stephen23 2019년 5월 27일
You can use tokens to identify the specific substrings and get their locations:
>> S = '>ABCD 2345678.123<';
>> [T,X] = regexp(S,'>(ABCD)\s(\w+)\.(\w+)<','once','tokens','tokenExtents')
T =
'ABCD'
'2345678'
'123'
X =
2 5
7 13
15 17
  댓글 수: 2
dsmalenb
dsmalenb 2019년 5월 27일
Just out of curiosity, if the string included the " or ' characters, then how would you need to handle the string?
Stephen23
Stephen23 2019년 5월 28일
편집: Stephen23 2019년 5월 28일
"...if the string included the " or ' characters, then how would you need to handle the string?"
What do you mean by "handle" ? It is not clear what your question relates to: how such as string should be defined, or how the regular expression should be written to also match strings which include single/double quotes ?
If your question was actually about how to detect/ignore single/double quotes when matcing the substrings, then please specify if you want to include them or ignore them in the output.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by