473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Help using Regular Expressions?

Is there a metacharacter that i can use to use to search for consonants? I need to search a file for words that have four consecutive consonants in them.

The file I am searching through is a story two paragraphs long. I also need to know how to extract the file word by word.
Oct 11 '06 #1
2 1877
geek491
21
Is there a metacharacter that i can use to use to search for consonants? I need to search a file for words that have four consecutive consonants in them.

The file I am searching through is a story two paragraphs long. I also need to know how to extract the file word by word.
using regular expressions would complicate things here i guess... what you can do is use substr(), lenght() functions and get your job done.

to read a file word by word Hmmmmmm.... lot of options but this one is simple

open(F1,"file.txt");
@lines = <F1>;
foreach $line(@lines)
{
@words = split(/ /,$line);
foreach $word (@words)
{
print "$word \n";
}
}

close (F1);

let me know how it helped to geek491@yahoo.co.in
Oct 13 '06 #2
miller
1,089 Expert 1GB
I agree with geek491, using regular expressions would simply complicate matters. This is one of those problems that you could solve with a masterful single regular expression, but you'd be wasting your time to bother writing it, and wasting computer resources to run it. Instead, working off of geek491's code, you can solve with the following code:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. my $inFile = $ARGV[0] or die "no file specified";
  4.  
  5. open(IN, $inFile) or die "open $inFile: $!";
  6.  
  7. my $wordCount = 0;
  8. my $count = 0;
  9.  
  10. while (my $line = <IN>) {
  11.     while ($line =~ m{([a-z]+)}ig) {
  12.         my $word = $1;
  13.         $wordCount++;
  14.         if ($word =~ m{[^aeiou]{4}}i) {
  15.             print "$word\n";
  16.             $count++;
  17.         }
  18.     }
  19. }
  20.  
  21. print "$count of $wordCount have 4 consonants\n";
  22.  
  23. close(IN) or die "close $inFile: $!";
  24.  
  25. 1;
  26.  
  27. __END__
  28.  
You'll notice two important changes to the code that I provided. First off, using split to determine the words in a line does not work completely because it will include punctuation characters. Secondly, I've included the code to test for 4 consonants. As there is not a character class in perl for this, I simply used "not a vowel" to test for this. I've also included a little counter to verify that this code actually ran just in case your text doesn't include any such words.

Finally, you'll have to decide for yourself if "y" is a vowel. You can include the word "demonstration" if your text doesn't have any long words and you want to verify that this script works.
Oct 16 '06 #3

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

Similar topics

9
by: Steve | last post by:
Hello, I am writing a script that calls a URL and reads the resulting HTML into a function that strips out everthing and returns ONLY the links, this is so that I can build a link index of various...
8
by: Johnny | last post by:
I need to determine whether a text box contains a value that does not convert to a decimal. If the value does not convert to a decimal, I want to throw a MessageBox to have the user correct the...
5
by: dennis | last post by:
Hi, First of all, hi to you all. I'm working on a Delphi project wich is becoming near it's deadline. I have a very simple XSLT question wich i hope one of you folks can help me with? The...
5
by: Greg Vereschagin | last post by:
I'm trying to figure out a regular expression that will match the innermost tag and the contents in between. Specifically, the string that I am attempting to match looks as follows: ...
2
by: news.microsoft.com | last post by:
I need help design a reg exp. I am parsing an html file to get the input values, here is one example <input VALUE="Staff Writer" size=60 type="text" name="author"> Can I grab the value "Staff...
13
by: The Cleaning Wonder Boy | last post by:
Could someone please explain to me what the (?<Key> and (?<Value> are in the following Regex expression? This gets relative links in an HTML string (file). ...
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
3
by: Zach | last post by:
I'm writing an app which is going to rely extremely heavily on the usage of regular expressions. I'm reading the docs but having trouble wrapping my head around some of this since it's all fairly...
23
by: codefire | last post by:
Hi, I am trying to get a regexp to validate email addresses but can't get it quite right. The problem is I can't quite find the regexp to deal with ignoring the case james..kirk@fred.com, which...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.