5

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.

6
  • 1
    What about (lo){3} ? Commented Jul 7, 2015 at 14:35
  • 3
    So more than 3 occurrences of lo are also good? Commented Jul 7, 2015 at 14:45
  • 3
    @anubhava I think it is but the exactly is confusing. Commented Jul 7, 2015 at 14:49
  • Sorry but in that moment i was in rage mode and i wrote something wrong, more than 3 is bad so "lolololoba" is not good! Commented Jul 7, 2015 at 15:25
  • ok I provided an answer to match only lines with exactly 3 lo at start. Commented Jul 7, 2015 at 16:42

4 Answers 4

6

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].)"
Sign up to request clarification or add additional context in comments.

Comments

3

You can use following regex :

^(lo){3}.*$

Instead of lo you can put your variable.

See demo https://regex101.com/r/sI8zQ6/1

5 Comments

No need for the .*$ - it doesn't do anything useful. Also, while this is a valid regular expression, it's not a complete answer without showing how to use it (e.g. with grep -E).
Whats the point of the \1 ? Why not just match the string 3 times ?
@TomFenech Yep you are right but I wanted to give a hint instead of the complete answer!
Hmm, why not just make your answer more useful?
Also note that this will match too much, e.g. it will match lolololoba which OP says is bad
0

You can use this awk to match exactly 3 occurrences of lo at the beginning:

# input file
cat file
lololoba
balololo
loloba
lololololoba
lololo

# awk command to print only valid lines
awk -F '^(lo){3}' 'NF == 2 && !($2 ~ /^lo/)' file
lololoba
lololo

Comments

0

As per your comment:

... more than 3 is bad so "lolololoba" is not good! 

You'll find that @Jahid's answer doesn't fit (as his gives you "good" to that test string.

To use his answer with the correct regex:

pat="^(lo){3}(?\!lo)"
s="lolololoba"
[[ $s =~ $pat ]] && echo good || echo bad

This verifies that there are three "lo"s at the beginning, and not another one immediately following the three.

Note that if you're using bash you'll have to escape that ! in the first line (which is what my regex above does)

1 Comment

I have added another pattern if you are interested.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.