I am stuck with following regex. my intention is to match following
abc_123_v1_f1_t21
12c_1sdsd_f1_t1
Android_v1_t21
regex i am trying is
_(v[1-9]{1,3}\d?_f[1-9]{1,3}\d?_t[1-9]{1,3}\d?)(?!\d)
I am not able to get regex match if my names doesn't contains squence (v_f_t). how to get regex match when i have pattern like without v or f or t ATLEAST two should be present. v_t or f_v or f_t or v_f_t or f_t_v etc
if I give adcg_f1_t1 it should match
if I give adcg_v2_f1 it should match
if I give adcg_v2_t12 it should match
How to do this ?
CodePudding user response:
To match strings that contain at least two of the substrings _v, _f, and _t, you can use the following regular expression:
.*(?:_v|_f|_t).*(?:_v|_f|_t).*
This regular expression uses the .* pattern to match any number of characters before and after the substrings _v, _f, and _t. The (?:_v|_f|_t) pattern matches either _v, _f, or _t, and the .* pattern that follows it matches any number of characters. The .* patterns and (?:_v|_f|_t) patterns are repeated to match at least two of the substrings _v, _f, and _t.
For example, the regular expression will match the following strings:
abc_123_v1_f1_t21 12c_1sdsd_f1_t1 Android_v1_t21 adcg_f1_t1 adcg_v2_f1 adcg_v2_t12 However, it will not match the string abc_123, since it does not contain at least two of the substrings _v, _f, and _t.
Here is an example of how you can use this regular expression in Python:
import re
pattern = r'.*(?:_v|_f|_t).*(?:_v|_f|_t).*'
string = 'abc_123_v1_f1_t21'
if re.match(pattern, string):
print('Match found!')
else:
print('Match not found.')
CodePudding user response:
You can use
(?:_[vft][1-9]{1,3}\d?){2,3}(?!\d)
See the regex demo.
Details:
(?:_[vft][1-9]{1,3}\d?){2,3}
- two or three occurrences of_
- an underscore[vft]
-v
/f
/t
[1-9]{1,3}
- one, two or three non-zero digits\d?
- an optional digit
(?!\d)
- no digit allowed immediately on the right.