In a bash script I have to match strings that begin with exactly 3 times with the string lo
; so lololoba
is good, loloba
is bad, lololololoba
is good, balololo
is bad.
I tried with this pattern: "^$str1/{$n,}"
but it doesn't work, how can I do it?
EDIT:
According to OPs comment, lololololoba
is bad now.
This should work:
pat="^(lo){3}"
s="lolololoba"
[[ $s =~ $pat ]] && echo good || echo bad
EDIT (As per OPs comment):
If you want to match exactly 3 times (i.e lolololoba
and such should be unmatched):
change the pat="^(lo){3}"
to:
pat="^(lo){3}(l[^o]|[^l].)"
You can use following regex :
^(lo){3}.*$
Instead of lo
you can put your variable.
See demo https://regex101.com/r/sI8zQ6/1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With