Fedora Linux Support Community & Resources Center
  #1  
Old 19th November 2012, 12:59 PM
Mainsun Offline
Registered User
 
Join Date: Feb 2012
Location: Hong Kong
Posts: 26
windows_7firefox
Discuss about regexp ...

Hi all,
Accounting to gnu/sed manual,
http://www.gnu.org/software/sed/manu...ar-Expressions
*
Matches a sequence of zero or more instances of matches for the preceding regular expression, which must be an ordinary character, a special character preceded by \, a ., a grouped regexp (see below), or a bracket expression. As a GNU extension, a postfixed regular expression can also be followed by *; for example, a** is equivalent to a*. POSIX 1003.1-2001 says that * stands for itself when it appears at the start of a regular expression or subexpression, but many nonGNU implementations do not support this and portable scripts should instead use \* in these contexts.
In my test.txt
Quote:
ab
aab
abb
a1234b
a???b
a b
a is not b
a
b
I want to replace all a*b to cd. So I run.
Quote:
sed 's/a*b/cd/g' test.txt
However, the output is not expected!
Quote:
cd
cd
cdcd
a cd
a is not cd
a
cd
Can someone state what 's wrong?
Reply With Quote
  #2  
Old 19th November 2012, 01:13 PM
Adunaic's Avatar
Adunaic Online
Registered User
 
Join Date: Mar 2009
Location: Lancaster, UK
Posts: 883
linuxfirefox
Re: Discuss about regexp ...

Perhaps you could post what the output is you were expecting.

* matches to zero or more instances of teh previous character / expression.

from your expression
Code:
sed 's/a*b/cd/g' test.txt
valid matches would include:
Code:
b (as zero a's)
ab (as one a)
aab (as more than one a).

Code:
ab   (matches as there is 1 a followed by a b. replaced with cd)
aab (more than one a followed by a b so replaced with cd)
abb (ab is matched and changed to cd. this leaves b which matches so is replaced by cd as well.)
a1234b (b is preceeded by zero a's so changed to cd)
a???b (b is preceeded by zero a's so changed to cd)
a b (b is preceeded by zero a's so changed to cd)
a is not b (b is preceeded by zero a's so changed to cd)
a (no match, must be followed by a b)
b (b is preceeded by zero a's so changed to cd)
Not sure why you did not get a match for lines 4 and 5. but it is doing what you want.

If you want it to match anything surrouned by a n 'a' and a 'b' you need to use:

Code:
a.*b
where the .* be means any and as many characters as there are.
Reply With Quote
  #3  
Old 19th November 2012, 01:29 PM
Mainsun Offline
Registered User
 
Join Date: Feb 2012
Location: Hong Kong
Posts: 26
windows_7firefox
Re: Discuss about regexp ...

thanks Adunaic ^_^.
your explanation is so clear!
Reply With Quote
Reply

Tags
discuss, regexp

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Where should we discuss Fedora 14 nimnull22 Using Fedora 8 2nd August 2010 08:15 PM
RegExp elf_aka Programming & Packaging 1 20th June 2008 11:40 AM
Problem with regexp in mod_redirect on Apache 2 locodude Servers & Networking 0 23rd April 2007 10:02 AM


Current GMT-time: 15:19 (Friday, 24-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat