This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # match 0 or more times using * quantifier | |
| echo "hello, how are you?" | grep -e "[a-z]*" # will match hello, how, are, you | |
| # match all numbers in chunks with length of one or more with + quantifier | |
| echo "47427 8381481 5813471" | grep -P "[0-9]+" | |
| echo "47427 8381481 5813471" | grep -P "\d+" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # using the ? means that we're not matching greedily | |
| echo "<p>Paragraph 1</p><p>Paragraph 2</p><p>Paragraph 3</p>" | grep -P "<p>.*?</p>" # will match <p>Paragraph 1</p>, <p>Paragraph 2</p>, <p>Paragraph 3</p> | |
| # we can use the ? to match an optional character | |
| echo "http:\\website.com,https:\\website2.com" | grep -P "https?.*" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # using the {x} will match whatever comes before it x amount of times | |
| echo "12341234444412354444" | grep -P "4{3}" # will match 3 times '444' | |
| # using the {x,y} will match a range from x to y | |
| echo "12341234444344412354444" | grep -P "4{1,4}" # will match 4, 4444, 444, 4444[ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # use {x,} to match string with x+ characters | |
| echo "hello world reallylongword" | grep -P "\w{6,}" -o # will match reallylongword |
NewerOlder