LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 09-27-2001, 01:34 PM   #1
gene_gEnie
Member
 
Registered: Aug 2001
Location: London UK
Distribution: Redhat 7.2, soon to be LFS!
Posts: 42

Rep: Reputation: 15
pattern matchin with perl


does anyone know how to match 2 concurrent characters with perl?
i.e. If the string was wheel, im searchin for 'ee', but i dont wanna specify 'ee'
like /ee/
i want something like>>

if (/(.)\1/)
this should work but for some reason it doesnt.

Anyone any ideas?
 
Old 09-27-2001, 02:05 PM   #2
gene_gEnie
Member
 
Registered: Aug 2001
Location: London UK
Distribution: Redhat 7.2, soon to be LFS!
Posts: 42

Original Poster
Rep: Reputation: 15
What im actually searching thru' is a large text file, and i want to match every occurence of double characters, and then substitute them with brackets surrounding them,

i.e. wh(ee)l

but, because im not just searching for ee, im searching for any double character, i.e.

"Hannah had a really nice car with four wheels"

Substituted with;

"Ha(nn)ah had a rea(ll)y nice car with four wh(ee)ls"

i cant use the explicit search like /ee/
 
Old 09-27-2001, 03:54 PM   #3
dorward
Member
 
Registered: Sep 2001
Distribution: Gentoo
Posts: 760

Rep: Reputation: 31
Erased


Last edited by dorward; 09-27-2001 at 03:56 PM.
 
Old 09-27-2001, 03:54 PM   #4
dorward
Member
 
Registered: Sep 2001
Distribution: Gentoo
Posts: 760

Rep: Reputation: 31
This is what I use in my PHP script to match 2 or more /s

/\/{2,}/

I think the syntax for a just two is the same, but without the comma.
 
Old 09-28-2001, 03:41 AM   #5
gene_gEnie
Member
 
Registered: Aug 2001
Location: London UK
Distribution: Redhat 7.2, soon to be LFS!
Posts: 42

Original Poster
Rep: Reputation: 15
nah, that doesn't seem to work,
what does work though is

if (/([^ ])\1/)

So i search and find all occurrences of double letters and do a substitution with this line

s/$&/$&/g;

but, it only replaces the first occurrence in the line from right to left
i.e.

ha(nn)ah had a really nice car with four wheels

my book - programming perl tells me to use a while loop like this

1 while s/$&/$&/g;

but when i runit, it just seems to hang

anyone?
 
Old 09-28-2001, 09:26 AM   #6
gene_gEnie
Member
 
Registered: Aug 2001
Location: London UK
Distribution: Redhat 7.2, soon to be LFS!
Posts: 42

Original Poster
Rep: Reputation: 15
got it!

but thanks in advance anyway

$_ = $lines;
chomp $lines;
if ($lines =~ s/([^ ])\1/(\1\1)/g)
{
$t++;
print $t . " $lines\n";
}
 
Old 09-29-2001, 11:40 PM   #7
odin
LQ Newbie
 
Registered: Sep 2001
Posts: 1

Rep: Reputation: 0
Cool

try:
s/((\w)\2)/\($1\)/g;
 
  


Reply



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
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
singleton pattern in C++? Thinking Programming 7 11-13-2005 07:45 PM
Perl Pattern Matching Question pete1234 Programming 2 08-27-2005 10:26 AM
pattern matching in perl ludeKing Programming 9 04-02-2004 09:53 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:31 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
Open Source Consulting | Domain Registration