LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-15-2008, 04:18 PM   #1
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,561
Blog Entries: 1

Rep: Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023
regular expression [tT]h*


Hi, I am playing with sed and was trying the following command:

Code:
less hwl | sed 's/[Tt]h*/((&))/'
I would think it will only put (( )) around instances of Th... and th....,
but it also put paranthesis around t... and T....

Quote:
Madame Sosos((t))ris, famous clairvoyante,
Had a bad cold, never((th))eless
Is known ((t))o be the wisest woman in Europe,
Wi((th)) a wicked pack of cards. Here, said she,
Is your card, ((th))e drowned Phoenician Sailor,
(((Th))ose are pearls that were his eyes. Look!)
Here is Belladonna, ((th))e Lady of the Rocks,
((Th))e lady of situations. 50
Here is ((th))e man with three staves, and here the Wheel,
And here is ((th))e one-eyed merchant, and this card,
Which is blank, is some((th))ing he carries on his back,
Which I am forbidden ((t))o see. I do not find
((Th))e Hanged Man. Fear death by water.
I see crowds of people, walking round in a ring.
((Th))ank you. If you see dear Mrs. Equitone,
((T))ell her I bring the horoscope myself:
One mus((t)) be so careful these days.
I thought it needs to be th or Th to meet the condition, not t or T on its own.

Where am I wrong?
thanks
 
Old 01-15-2008, 04:36 PM   #2
urka58
Member
 
Registered: Nov 2003
Distribution: slackware 14
Posts: 530

Rep: Reputation: 30
You should remove the "*" as it substitutes 0 or more instances of h, so Th, th, t, and T will be substituted.
I'd also put the "g" option at the end of the sed expression otherwise only the first occurrence on the line of the buffer will be substituted.
ie the last line of your file has two occurences of the buffer, but only one is substituted
One mus((t)) be so careful ||th||ese days.
Ciao

Last edited by urka58; 01-15-2008 at 04:50 PM.
 
Old 01-15-2008, 04:41 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
The problem is the asterisk: it means that the preceding regular expression should be repeated as many times as necessary to find a match, that is your regex will match any occurrence of 'T' or 't', followed by any number of 'h'. Including zero.

Just remove the asterisk and it will work in the expected way:
Code:
sed 's/[Tt]h/((&))/g'
here I also added the g specifier to match the multiple occurrences on the same line (as in the 6th one of your example).

Edit: sorry for redundancy... I did not see your post, urka58 before posting mine.

Last edited by colucix; 01-15-2008 at 04:45 PM.
 
Old 01-15-2008, 04:43 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,561
Blog Entries: 1

Original Poster
Rep: Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023Reputation: 1023
thanks, I get it now
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Do we have regular expression in C++ ? indian Programming 4 03-06-2006 09:54 AM
Regular expression datbenik Programming 1 01-05-2006 01:58 PM
Need help with regular expression aecaudel Programming 6 11-04-2005 05:28 AM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 10:08 AM
Regular Expression slizadel Programming 4 07-28-2003 05:16 AM


All times are GMT -5. The time now is 05:45 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration