January 19, 2008

Regex tips

1. ?: => To not to capture the text matched within the parenthesis.
2. /i => Case insensitive
3. perl -p -i -e 'some command' file => -e program is in command line -p substition to be done for every line in the file -i changes to be written back to the file when done
4. $line =~ s/^/|> /; => Make every line start with |>
5. ?=\d => Positive lookahead, check if next char is \d without moving cursor
6. ?<=\d => Positive lookbehind
7. ?! => Negative lookahead, check if next char does not match
8. ? Negative lookbehind
9. e.g. s/(?<=ABC)(?=EFG)/D/g => inserts D
10. /x => Free format regex, turns whitespace into ignore me and # into comment. Really helpful for writing complex regexes spanning multiple lines.

1 comment:

Incognito said...

How to get the text with regex , the text that is between * and * in a text file if it's length is more that 10 characters ?

Example text contains:

*
some text here
*
*some text here*

*some text here
*

*thisnot*