Link to home
Start Free TrialLog in
Avatar of cer113
cer113Flag for Canada

asked on

How to use Regular expression

Hi

How would i use regular expressions to change text in vi. For example I have file.txt file and I want to change word block to unblock.  Please give me whole syntax since I am begginer with Regular expressions. I found some stuff on google but it is all too advanced. I dont understand people that write things for experts cause for the hell sake expert doesnt need instructions. But for begginer there is nothing on the whole internet how to learn regular expressions.

How would i replace above mentioned word in text file

:%s/\<block\>/unblock/g I tried this as advised by one guy but bash is giving me error

Avatar of farzanj
farzanj
Flag of Canada image

:%s/block/unblock/g
ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cer113

ASKER

yes this was working all good word is replaced i opened vi file.txt and in command line typed the command and word got replaced that is good
WARNING: farzani's tip works but will replace block with unblock even if it is not a whole word. For example if your file read:
What kind of block does a blockbuster bust?

Open in new window

you would get:
What kind of unblock does a unblockbuster bust?

Open in new window


To avoid this problem, try this instead
:%s/\bblock\b/unblock/g

Open in new window


\b asserts "word boundaries" (non-word characters or start of line/end of line)

A good tour of Regular Expressions is PHP's http://www.php.net/manual/en/regexp.introduction.php  actually based on Perl's Regular Expression, which is a superset of the basic regular expressions in every Unix.