Saturday, June 13, 2009

Regular Expressions in Powershell

Regular Expressions are the most powerful pattern matching techniques available for text parsing. Almost all programming languages have regular expressions. Perl has very powerful regex engine and it provides more features.

Windows Powershell carries excellent regular expression support from .Net Framework

Regex Pattern matching in Powershell is so easy. We will go though some examples on Regular Expressions in Windows Powershell

Prerequisite: Some knowledge on regular expression would be good


PS C:\Scripts> 'Hello World' -match '^H'
True
PS C:\Scripts> 'ello World' -match '^H'
False

Powershell returns True if the match is found, otherwise False is returned

Let's see one more example


PS C:\Scripts> '2009-Jun-13' -match '\d{4}-[A-z]{3}-\d{2}'
True
PS C:\Scripts> '2009-June-13' -match '\d{4}-[A-z]{3}-\d{2}'
False

-replace operator is used for Search and Replace in Powershell


PS C:\Scripts> '2009-June-13' -replace "2009", "2010"
2010-June-13

How do you capture the pattern and use it in Search and Replace in Powershell? Lets see how,


PS C:\Scripts> '2009-Jun-13' -replace "(\d{4})-([A-z]{3})-(\d{2})", "$2 $3 $1"

PS C:\Scripts> '2009-Jun-13' -replace "(\d{4})-([A-z]{3})-(\d{2})", '$2 $3 $1'
Jun 13 2009

In Search and Replace, Powershell stores captured patterns in special variables $1, $2, etc. But if you use those special variables inside double-quoted replacements, they will be considered as normal variables. That's the reason, we didn't get expected result in first statement in the above code window. To make it work, you need to use single-quoted replacements like above.

To get more idea on this, let's see another example


PS C:\Scripts> $1="How"
PS C:\Scripts> $2="are"
PS C:\Scripts> $3="you"
PS C:\Scripts>
PS C:\Scripts> '2009-Jun-13' -replace "(\d{4})-([A-z]{3})-(\d{2})", "$2 $3 $1"
are you How
PS C:\Scripts> '2009-Jun-13' -replace "(\d{4})-([A-z]{3})-(\d{2})", '$2 $3 $1'
Jun 13 2009

Now let's see how to use captured patterns with -match operator. If you capture patterns using -match, unlike Search and Replace, the captured patterns will be stored in a special array variable called $matches.


PS C:\Scripts> '2009-Jun-13' -match '(\d{4}).*'
True
PS C:\Scripts> $matches

Name Value
---- -----
1 2009
0 2009-Jun-13

PS C:\Scripts> $matches[1]
2009
PS C:\Scripts>

Regular Expressions in Powershell are case-insensitive by default. For case-sensitive pattern matching, we can use the following operators

1. -cmatch
2. -creplace


PS C:\Scripts> 'HELLO' -match '[A-Z]'
True
PS C:\Scripts> 'HELLO' -match '[a-z]'
True
PS C:\Scripts>
PS C:\Scripts> 'HELLO' -cmatch '[A-Z]'
True
PS C:\Scripts> 'HELLO' -cmatch '[a-z]'
False
PS C:\Scripts>

10 comments:

  1. Regular expression is a technique used for parsing. The concept of regular expression is not easy to understand. It requires lot of concentration. You need to get the basics accurately. This post does the work for those who want to learn regular expressions in powershell.

    ReplyDelete
  2. Yes. Those people should buy a book

    ReplyDelete
  3. Playing online casino games is convenient and fast.
    Ruby888The site is open for everyone to enjoy fun online games from various options from simple and easy to play anywhere at any time via mobile phones and screens. Your computer In the form of Download to install the machine. Then click play through the game icon. Or you can log in through our online betting site directly. It is convenient as well. But to play Gclub Online games through the site will not be able to play games in the slots. Gclub online gambling is good for new subscribers, receive a welcome bonus of 200 Baht minimum opening account. All customers who can support you to study the details of the online casino Gclub online at our online gambling site at any time. If you still have any questions, please contact us. operator Our direct it. The way our feature works. Ready to serve you 24 hours a day, please visit us at the website. IBCbet

    ReplyDelete
  4. Online Casino Service Channels
    GclubOnline casino games allow players to enjoy and meet the needs. Winning online games of the highest pleasure will bring the next bet to the customer. We are also a betting channel for online games that offer comfort. And fast service. Whether it's a game or a hit, it does not make you happy. Because of that, online gambling sites are like online gambling games. Because the game has nearly 100 games for customers to choose to gamble. They also bet on their favorite online gambling games. Gambling online is an online gambling that makes you gambling in real time. Because of the live broadcast through clear camera, it makes the mood of fun and better realism. The user is gambling stable and comfortable to play by playing our site directly because they do not need to download. Gambling online with all kinds of fun for you. No matter what kind of user you want to play games, we will definitely serve you. Gclub มือถือ

    ReplyDelete
  5. The most popular online casinos service.
    คาสิโนOnline casino games service sites that allow people to entertain and meet the needs. Online casino games are the ultimate in entertainment that will bring better bets to players. And we also play online casino games that are convenient. And fast service. Whether it is a game or popular, it does not make you miss fun. Because we are the same online casino games. Which brings up nearly 100 games for you to choose the game you want. Also, play your favorite online casino games 24 hours a day. Use the live online casino games that allow you to play in real time. This is a clearer and more realistic way of transmitting through the camera. You can play safely and easily accessible by playing through our website directly without having to install the program. Online casinos with all kinds of information for you. No matter what type of game you want to play, we can definitely answer your questions. คาสิโนออนไลน์

    ReplyDelete
  6. The challenge of monetizing new ways
    G club It is a website that is open to the gambling in the online casino system. The games that are open to all the games that are so popular as blackjack, poker, roulette and slots are all new games for gamblers around the world. In addition, the computer network of our network also has a management system is also universal. The game can be very convenient for players to risk. You can bet on the g-club 69 of us immediately. It also has a prototype of the most up-to-date games. Websites that do not have the flexibility to make a lot of gamblers to use the service. Great online gambling services are available through the website. Players can place their bets without having to download complicated programs. In addition, players can also watch the risk through the website immediately. The problem with downloading and installing the program is difficult. Do not wait, hurry up and join us in the comfort of today. Come to us. จีคลับ

    ReplyDelete
  7. Mobile relaxation channel
    Sbobet Our online casino site is a place to entertain. To get online gamblers To play games, bet. To save time in traveling to a real casino abroad. Because our site is easily accessible. Just have a cell phone or computer connected to the internet. This makes it easy to play anywhere anytime you want. It is also a leader in the fun for the player to join our site. In addition to enjoying the variety of games available. It will be fun to play with the game by simulation games. It comes from online casinos. There are also a lot of gaming bonuses where players can use their expertise to play with our site. And if there are doubts or obstacles during the game. We have a 24 hour concierge and are always on hand. Please join us at our website. คาสิโน

    ReplyDelete
  8. จีคลับ Online gambling sites will give you full play. Which can play well. Make more money. Do not waste time. Stay at home at work, there are many gambling games lost. Who likes or wants to experience a game of gambling. It will play anywhere. Get the good stuff. There are bets to like to make more returns. Absolutely true to every match.

    In addition, it is interesting. Avocado seeds It has beneficial effects on the body, such as promoting the muscular and nervous systems. Inside, avocado seeds are rich in potassium, an important mineral that promotes the functioning of the musculoskeletal system. Help maintain blood pressure as well. Relieve inflammation, swelling, antioxidants in avocado seeds. Procyonides Features reduce pain. Reduces muscular exhaustion. Reduces inflammation of the joints and bones.

    It is interesting to have a betting game that will play itself in all areas. Gambling every day. With the play that everyone likes. Who would like to gamble themselves. Always enjoy the fun. Can play well Make more money. To play all areas. Can play as easily as a gambling game is one. Experience a great gambler game every day. บาคาร่า

    ReplyDelete
  9. Your blog is very nice,Thanks for sharing good blog.

    หนังตลก

    ReplyDelete