Tips n Tracks

  • Increase font size
  • Default font size
  • Decrease font size
  • default color
  • black color

Reference

Sample image

Microsoft .NET Framework Get Details.

Sample image

Microsoft .NET Framework Get Details.

Reference

Sample image Microsoft .NET Framework Get Details.
Sample image

Microsoft .NET Framework Get Details.


Understanding Regular Expressions metacharacters with uses


Understanding Regular Expressions metacharacters with uses

The ^ (caret) metacharacter – Start of a string

^ (caret) metacharacter matches the characters that occur immediately after the beginning of a line or string.

Suppose we want to search "we"
Text Pattern is – we have holiday on wednesday

It will match the sequence of characters "we" in the words we, and wednesday. However, the same pattern preceded by the ^ metacharacter means "^we" when applied to the same Text Pattern. It will match the sequence of characters "we" occurs immediately after the start of the string.

The $ (dollar) metacharacter – End of a string

$ (dollar) metacharacter matches the characters that immediately precede the end of a line or string.

Suppose we want to search "day"
Text Pattern is – Today we have office but we have holiday on wednesday

It will match the sequence of characters "day" in the words Today, holiday and wednesday. However, the same pattern processed with the $ metacharacter means "day$" when applied to the same Text Pattern. It will match the sequence of characters "day" occurs at the end of a line in the words wednesday.

. (the period or dot ) – Any character (except \n newline)

The . (period or dot) is one of the most broadly scoped metacharacters which can match any character (lowercase or uppercase) as well as any numeric digit.

Suppose we want to search with "."
Text Pattern is – we have holiday on wednesday

It will match all the sequence of characters "we have holiday on wednesday". Now we search for ".a". It will return match "ha" in have, "da" in holiday, and "da" in wednesday.

| (pipe) – Alternation: state to another and back again

The | (pipe) metacharacter which conveys the notion of alternation or the logical OR which allows specific options to be matched.

Suppose we want to search for "sunday or monday"
Text Pattern is –
sunday have holiday.
monday is working day.

Here we have we use "sunday|monday". It wills return match "sunday" from first line and "monday" from second.

{…} (Curly Brace) – Explicit quantifier notation.

If you want to specify large numbers of occurrences, you can use a curly-brace syntax to specify an exact number of occurrences.

Suppose we want to search for "user11, user21,… user91" means occurance of 0 to 9 after user two times
Text Pattern is –
user1
user81
user4
userX1
user67

Here we have we use "user[0-9]{2}". It will return match "user81" and "user67".

[...] (square bracket) – Explicit set of characters to match.

If you want to specify specific range of charcter,number, you can use a [...] (square bracket) syntax to specify an exact range.

Suppose we want to search for "user0, user1, user2, user3".
Text Pattern is –
user1
user81
user4
userX1
user67

Here we have to use "user[0-3]". It will return match "user1".

(…) (Parentheses or the round bracket) – Logical grouping of part of an expression.

Parentheses create one or more groups of matched characters which can be used in the text manipulation.

Suppose we want to search words contails "day"
Text Pattern is – we have holiday on wednesday
"(day)" will return us "day" form holiday and wednesday.

* (star or asterisk) – 0 or more of previous expression.

The * operator refers to zero or more occurrences of the pattern to which it is related.

Suppose we want to search for "user, user1, user11, user2, user3".
Text Pattern is –
user1
user81
user4
userX1
user67

Here we have to use "user[0-3]*" will return match as per our requirement.

+ (plus) – 1 or more of previous expression

The + operator match one or more occurrences of the character or group of characters is present at least once but also allow for the possibility that the character occurs more than once.

Suppose we want to search for "user1, user11, user111 etc".
Text Pattern is –
user1
user11
user4
user111
user67

Here we have to use "user1+" will return match "user1", "user11" and "user111" as per our requirement.

? (question mark) – 0 or 1 of previous expression

The ? metacharacter signifies a any single character.

Suppose we want to search word like "rat or at".
Text Pattern is –
rat
matter
cat
bat
basket

Here we have to use "r?at" will return match "rat" and "at" form matter, cat, bat respectively

\ (backslash) – escape sequence

All metacharact have some special meaning and use. But there are some situations where we want to search for it. Like we want to search for currence doller($). Here escape sequence backslash escaped it to use them to match the corresponding (doller $) literal character.

We want to search word like "$ or $$".
Text Pattern is –
$1
$am
$15
ha$$le

Here we have to use "\$+" will return match "$" form $1, $am, $15 and "$$" form ha$$le

 

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

[...] Understanding Regular Expressions metacharacters with uses [...]

Pingback by Text and Data Manipulation with Regular Expressions in .NET Development | Tips n Tracks on April 14, 2010 @ 2:44 pm

Leave a comment

(required)

(required)

*
To prove that you're not a bot, enter this code
Anti-Spam Image