How can I match a tab to the end of the line with a Perl regex?
I have a file with the below contents:
After learning everything you've learned so far, you may think you've bingo: got a pretty good foundation in programming Perl, since you'd already be a good way through most of the concepts many other languages entail. endbingo: But if you put down this book today and did nothing else bingo: with Perl beyond what I've already taught you, you'd miss endbingo: thats ok.
I need a Perl regular expression to match the lines "bingo: got a pretty good foundat开发者_运维百科ion in" and "bingo: with Perl beyond what I've already taught you"..
In the sense, the word "bingo:followed by a tab, followed by any random set of characters till the end of line".
Since you have not posted any code of your own, I will presume you are not even sure how to begin to construct a Perl regular expression. Here are some resources to get you started.
From the official Perl documentation website:
- perlintro
- perlrequick
- perlretut
I realize that this does not directly answer your question (as others have already done), but perhaps it will help you to converge more quickly on a solution to a future problem.
In multiline mode try:
\Wbingo:\s.*$
\W
means any non alphanumeric char
\s
means white char (space, tab, new line)
.*
means zero or more random characters
$
means end of line
if by match, you just want to show the lines with "bingo:" and tab onwards, then
perl -ne 'print if /bingo:\s+.+$/' file
if you want to match the word "bingo:" but not "endbingo:", then
$ perl -ne 'print if /\bbingo:\s+.+$/' file
you may think you've bingo: got a pretty good foundation in
bingo: with Perl beyond what I've already taught you,
If you are looking a tab and not all white space use the following:
/bingo:\t.*$/
If the line must start with "bingo:" you should use this:
/^bingo:\t.*$/
So it should not match endbingo ? Try this:
/(?<!end)bingo:\s+.*$/
the (?<! is a negative lookbehind and is the proper way to exclude the endbingo without excluding cases where the "bingo:" immediately follows some non-blank text.
Otherwise, if there is always whitespace behind the bingo, just do /\sbingo:\s+(.*)$/
精彩评论