Unbelievable Regex (part 1)

17 Sep

A regular expression allows you to efficiently search for strings within other strings using a pattern matching expression.Common uses include searching (matching) and search-and-replace. You can also use common expressions to test for specific conditions in a string, text file, Web page, or XML stream. You could use regular expressions as the basis for a program that filters spam from incoming mail.

Character Matching
. Matches any single character except the newline character.
[ ] Matches any one of the enclosed characters. You can specify a range using a hyphen, such as [0-9].
x|y Matches either x or y.
Position Matching
^ Matches beginning of string.
$ Matches end of string.
Repetition Matching
? Matches 0 or 1 instances of the preceding character.
+ Matches 1 or more instances of preceding character.
\ Indicates that the next character should not be interpreted as a regular expression special character.
* Matches 0 or more instances of preceding character.

examples:

tak.note

will match strings like takenotetakznotetak1note,

takenote[123]

will only match takenote1takenote2, and takenote3and will not match a similar string like takenote0

[0-9]takeNote

[T|t]ake[N|n]ote

will match the strings TakeNoteTakenotetakeNote,or takenote but will not match similar strings like wakenote

^take

This expression will match takenote and take a note but will not match I think ‘I’ve been taken by ‘

[T|t]ake[N|n]ote$

This expression will match I work for TakeNote but will not matchTakeNote is who ‘I work for ‘

^takenotes?

match takenote and takenotes

^takenote+

match takenote and takenoteee

……………………………………………………………………………….

To checkout the Regex use :

grep [pattern]  [file]

eg:

grep ^takenote$ test.txt

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: