By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,848 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,848 IT Pros & Developers. It's quick & easy.

regexp question :

P: 51

i'd like to detect all the lines that their first alpha-numeric word repeats more then once.

for example: this line would be 'hits'
abc sda asd asd abc
aaa bbb ccc aaa aaa

Mar 6 '08 #1
Share this Question
Share on Google+
3 Replies

Expert 100+
P: 971
Check out the perldoc's. You could also shows us what you tried.

Mar 6 '08 #2

P: 51
unfortunately i only find how to do it in more than one code line.
meaning that at first i extract the first word, and then i make the replacements.

$string =~ m/(\w+)/;
$string =~ s/$1/$replace_string/;

i don't know how to count the number of occurrence of the first line. so i'll hit only the lines that their first word repeats itself at some point.

i also believe that there is more elegant way to do it.

Mar 6 '08 #3

Expert 100+
P: 410

i don't know how to count the number of occurrence of the first line.
You want to count number of occurences of first line or occurence of first word in each line?
If you want to count occurences of first word and to display only the hits, you can use
Expand|Select|Wrap|Line Numbers
  1. open(IN,"input.txt") or die "sorry:$!";
  2. while(<IN>)
  3. {
  4.    /^(\w+)/;           ## same as $_ =~/^(\w+)/;
  5.   my $str=$1;
  6.   my $c=s/$str/$str/g;   ## similar to $c=$_=~s/$str/$str/g;
  7.   print "{count=$c}:$_ " if($c>1);
  9. }
Mar 7 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.