Can I use a character as a delimiter of a range in regex? For example if I want the range to start index 1 and ends at character $. How can I do that?
{1, what 开发者_如何学Cdo i put in here}
this regex will match any letter, number or the $ sign. is that all you need?
[A-Za-z0-9\$]
Update:
given OP's comment to match "$CM-PRQRPMD$", use the following regex:
\$[A-Z-]+\$
You don't. Just use +?\$
. For example, .+?\$
matches at least one character up to and including the first $
. (The $
cannot be the first character, though; for that, simply use .*?\$
.)
If you want to exclude the $
, wrap it in a lookahead assertion: .+?(?=\$)
.
EDIT (in response to clarification): To match identifiers between a pair of $
, and assuming the $
s belong to the identifier, you can just write this: \$(.*?)\$
. The parentheses will create a capturing group, so that the $
s are excluded from the match. If the delimiters aren't part of the identifier and you need to find multiple matches — e.g., $AAA$BBB$
— then you'll need to use zero-width assertions: (?<\$)(.*?)(?=\$)
so that the $
s aren't absorbed while matching.
精彩评论