开发者

Ruby regex, is there a way to only match literal matches?

开发者 https://www.devze.com 2023-01-31 10:33 出处:网络
I\'m trying to parse using a case/when statement with regex in it. I\'m having some trouble with the match as it will give me a match even if it\'s not a literal match.

I'm trying to parse using a case/when statement with regex in it. I'm having some trouble with the match as it will give me a match even if it's not a literal match.

Example:

if I input ($45, x), I get back: "address mode: indirect, x -> value: 45" from this regex:

/[(][$][1-9a-fA-F]{1,2}\s*,\s*[xX]\s*[)]/

Now, if I input ($45, p), I get a match for this regex:

/[$][1-9a-fA-F]{2,4}/

Which is understandable, but I would like the match to look only for literal matches. If there are extra characters that does not exactly match the regex I want the match function to return false.

Is there some other functions like match() or extra arguments that can be given to match() to get this b开发者_C百科ehavior?


From your question, it is a little unclear what you are after. Your second regex is matching on the substring

$45

If you want to avoid this, use the anchors ^ and $ to ensure the entire string is matched. Something like:

^\(\$[1-9A-Za-z]+,\s*[xX]\s*\)$
0

精彩评论

暂无评论...
验证码 换一张
取 消