How do I write a regular expression to match the following
CONTEXT_84 =
or
CONTEXT_5 =
thanks
How do I write a regular expression to match the following
CONTEXT_84 =
or
CONTEXT_5 =
thanks
it depends on your target language, but the main difference between the two is the numbers, so you can do this to get 'CONTEXT_' with at least one number followed by a space and an '=':
CONTEXT_[0-9]+ =
or this, to get 'CONTEXT_' with min of one, max of two numbers, followed by a space and an '=':
CONTEXT_[0-9]{1,2} =
Hi Matt,
try
CONTEXT_\d{1,2} =
Which means:
CONTEXT_\d{1,2}
Match the characters “CONTEXT_” literally «CONTEXT_»
Match a single digit 0..9 «\d{1,2}»
Between one and 2 times, as many times as possible, giving back as needed (greedy) «{1,2}»
Created with RegexBuddy
Your question already contains the answer: you ask
how do I match
CONTEXT_84 =
orCONTEXT_5 =
?
That's already all you need, the only thing missing is how to say or in Regexp, and that's |
.
So, your solution is
CONTEXT_84 =|CONTEXT_5 =
You can shorten that by pulling out the common parts:
CONTEXT_(84|5) =
And you're done!