dublintech
dublintech

Reputation: 17785

Regular expression for might have a space

I am trying to look for a regular expession for might have a space. I am searching for occurences of

word1word2

and

word1 word2 

I try:

grep "word1[ +]word2" mylog.txt

But this does not work.

Any ideas?

Upvotes: 1

Views: 105

Answers (4)

Jonas Elfström
Jonas Elfström

Reputation: 31428

Try

grep "word1 \?word2" mylog.txt

Upvotes: 1

Slomo
Slomo

Reputation: 1234

Try "word1\s*word2".

  • usually means 'at least one, but also many occurences'.
  • means 'any amount of occurences, even zero' \s is the placeholder for whitespace characters (can also be a tab).

If you would only want zero or one space you could try "word1\s{0,1}word2"

Btw: the + or * would follow after the brackets: [ ]+

Upvotes: 0

pietervp
pietervp

Reputation: 255

Following regex should do the trick:

x *y

It will check for any number of spaces between x and y.

Upvotes: 1

tofcoder
tofcoder

Reputation: 2382

Try

grep -E "word1 ?word2" mylog.txt

Upvotes: 3

Related Questions