Extract regexp tokens with regexpPattern
조회 수: 4 (최근 30일)
이전 댓글 표시
With regexp I could extract the tokens of my capture groups via
regexp("abcd3e", "\w+(\d)+\w", "tokens")
The result is a cell array. With the new regexpPattern and extract functions, the return values usually are string (arrays) which is something I prefer.
Question: Is there an analogon of the above regexp using something like extract("abcd3e", regexpPattern("\w+(\d)+\w"), "tokens")? This syntax obviously does not work in R2023b, but are there standard ways to rewrite these patterns to return my tokens?
Thanks,
Jan
EDIT: this is just a toy example, I do not only want to extract digits which could be done with digitsPattern. Ideally, I'd like to understand how directly translate the regexps.
To show a more realistic example:
str = [
"42652Z_HEX"
"42652X"
"42652Y"
"42652Z"
"42652GYRO-X_HEX"
"42652GYRO-Y_HEX"
"42652GYRO-Z_HEX"
"42351Temp_HEX"
"42652Temp_HEX"
"42652GYRO-X"
"42652GYRO-Y"
"42652GYRO-Z"
"42351Temp"
"42652Temp"
];
res = string(regexp(str, "\d+(?:GYRO-)?([XYZ])?.*", "tokens"))
% how to get the same result with matches and regexpPattern?
댓글 수: 2
Dyuman Joshi
2024년 2월 29일
이동: Dyuman Joshi
2024년 2월 29일
If you just want to extract numbers between letters -
str = "abcd3e57xyz";
out = extract(str, digitsPattern)
답변 (1개)
the cyclist
2024년 2월 29일
I realize that this is not really an answer to your question, but I just wanted to make sure you are aware that one option is to wrap the string function around the regexp:
string(regexp("abcd3e fghi4j", "\w+(\d)+\w", "tokens"))
Also, if you are guaranteed to have only one match, you could do
regexp("abcd3e", "\w+(\d)+\w", "tokens","once")
but that's somewhat fragile coding, I would say.
I'm not yet sure if there is a more "direct" way with more recent functions.
댓글 수: 2
the cyclist
2024년 2월 29일
Your updated question clarifies that my answer is not what you are looking for, but I'll leave it here anyway. :-)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!