LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-21-2009, 12:10 AM   #1
ahtoot
LQ Newbie
 
Registered: Jun 2005
Posts: 22

Rep: Reputation: 1
How to write this regular expression


I'm currently in Vim and have a few dozen lines that look like this:

Code:
.................([word]).............
The (...) on both sides are equal on all lines, and only the "word" is different. I want to replace the line to look like this:

Code:
.................([w,o,r,d]).............
Any idea how to write the substitution pattern?
 
Old 03-21-2009, 12:17 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
:s/[[:alpha:]]/,&/g
:s/[,\([[:alpha:]]\)/[\1/

The first command will result in
([,w,o,r,d])
The next line changes [, to [

Last edited by jschiwal; 03-21-2009 at 12:19 AM.
 
Old 03-21-2009, 01:27 AM   #3
ahtoot
LQ Newbie
 
Registered: Jun 2005
Posts: 22

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by jschiwal View Post
:s/[[:alpha:]]/,&/g
:s/[,\([[:alpha:]]\)/[\1/

The first command will result in
([,w,o,r,d])
The next line changes [, to [

The '[' in the second command has to be escaped
Code:
:s/\[,\([[:alpha:]]\)/[\1/
That works fine if the lines consisted of just
Code:
([word])
But my lines have stuff in front and behind:
Code:
lines([word1]).
lines([word2]).
lines([word3]).
which I wish to transform to:
Code:
lines([w,o,r,d,1]).
lines([w,o,r,d,2]).
lines([w,o,r,d,3]).
The problem I have is ignoring the "lines" and "." and work only on the characters between "([" and "])"
 
Old 03-21-2009, 01:55 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
Thanks atoot. I had made the correction when I did it but copied my work wrong.

You are changing the input pattern now.
Because the patterns before parenthises matches the patterns inside,
You may need to resort to using sed and the Hold register, or have a rule for various lengths between the brackets.

What is this for? Could you give some real lines?
 
Old 03-21-2009, 02:33 AM   #5
ahtoot
LQ Newbie
 
Registered: Jun 2005
Posts: 22

Original Poster
Rep: Reputation: 1
Well, I'm coding Prolog, and I need to add some rules to encode a crossword puzzle.

Essentially, each word is encoded as a list as follows:

word([l,a,d,y]).
word([f,a,s,c,i,n,a,t,e]).
word([b,l,u,e).
....

I already had the words in a list:

lady
fascinate
blue
...


Then I used a substitution to replace them as:

word([lady])
word([fascinate])
word([blue])


Then I was wondering how I could add the commas between each character.
Actually I had already done the substitution, by first adding the commas

l,a,d,y
f,a,s,c,i,n,a,t,e
b,l,u,e


Then inserting "word([" and "])"


But I'm asking for the sake of asking, just in case I encounter something similar in the future


[Edit] Btw I forgot to thank you for your help

Last edited by ahtoot; 03-21-2009 at 02:34 AM.
 
Old 03-21-2009, 06:45 PM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
It would be easier to use sed to manipulate the original list.
E.G.: sed 's/[[:alpha:]]/,&/g;s/^,//' wordlist
l,a,d,y
f,a,s,c,i,n,a,t,e
b,l,u,e

Since you have the original word list, you could use sed to convert use original word list to construct a sed program containing statement for each word, e.g.
...
s/(elephant)/(e,l,e,p,h,a,n,t)/
s/(house)/(h,o,u,s,e)/
...
You could use the Hold buffer and come up with a way to deconstruct the line to [(<the_word>)], make the substitution and the reconstruct it.
It would take a lot of juggling however. Using your original word list to construct the lines would make more sense IMHO.

Last edited by jschiwal; 03-21-2009 at 06:46 PM.
 
  


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
regular expression (.*?) uttam_h Programming 6 05-30-2008 06:45 PM
Need help with a regular expression abefroman Programming 2 05-28-2008 10:17 PM
about regular expression '=~m//' littletransformer Programming 7 03-25-2008 09:31 PM
Regular expression datbenik Programming 1 01-05-2006 02:58 PM
HOw do I write this regular expression linuxmandrake Programming 16 10-15-2005 05:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:46 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration