can arrays be used in regular expressions?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting can arrays be used in regular expressions?
# 1  
Old 04-14-2004
can arrays be used in regular expressions?

anyone know if there is a way to use an array in a regular expression. suppose i want to find out if $sometext contains one of an array's elements. Obviously the code below doesn't work, but is there something similar that does work?

@array = qw/thing1 thing2 thing3/;

if ($sometext =~ /(@array)/) {
print "$1\n";
}


basically i've got a big list of words to search for, and i'd like to avoid something like this:

($sometext =~ /(thing1|thing2|thing3)/)
# 2  
Old 04-14-2004
well your not really useing the array as the regx your useing a regex to find 1 element in your array.

you would end up doing it so the regex loops over the each element in an array.

check out perls map and grep functions.

here is a quick example.
my @nfiles=grep /my search pattern/, @files;

if you use the array @nfiles as your collection the values pulled from @files will populate the @nfiles.

if you use @nfiles as a scallar ie: $nfiles you will get the number of times you found a match vs. each match.

same things w/ map. except the map function can have a function to process your inbound data.


EDIT:

ok i think i know what your trying to do now.

and the answer is still yes.

populate your array then do a loop to itterate over each element of your array inserting it as the search pattern

something like

[code]
for (@words) {
if ( $sometext =~ /$_/ ) { print "I FOUND IT BOB: $_\n" );
}

Last edited by Optimus_P; 04-14-2004 at 05:18 PM..
# 3  
Old 04-14-2004
excellent, the solution in your edit works great

thanks OP
# 4  
Old 04-14-2004
you were already on the money i was supprised you didnt see it as you were typeing it out.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Regular Expressions

I am new to shell scripts.Can u please help me on this req. test_user = "Arun" if echo "test_user is a word" else echo "test_user is not a word" (1 Reply)
Discussion started by: chandrababu
1 Replies

2. Shell Programming and Scripting

Help with regular expressions

I have a file that I'm trying to find all the cases of phone number extensions and deleting them. So input file looks like: abc x93825 def 13234 x52673 hello output looks like: abc def 13234 hello Basically delete lines that have 5 numbers following "x". I tried: x\(4) but it... (7 Replies)
Discussion started by: pxalpine
7 Replies

3. Shell Programming and Scripting

Regular expressions help

need a regex that matches when a number has a zero (0) at the end of it so like 10 20 120 30 330 1000 and so on (6 Replies)
Discussion started by: linuxkid
6 Replies

4. Shell Programming and Scripting

Test Regular Expressions on Arrays in Awk

How would I test for a suffix on an element in an array? e.g. testing for /$html/ of an element array (4 Replies)
Discussion started by: ROFL
4 Replies

5. UNIX for Dummies Questions & Answers

regular expressions

how to find for a file whose name has all characters in uppercase after 'project'? I tried this: find . -name 'project**.pdf' ./projectABC.pdf ./projectABC123.pdf I want only ./projectABC.pdf What is the regular expression that correponds to "all characters are capital"? thanks (8 Replies)
Discussion started by: melanie_pfefer
8 Replies

6. Shell Programming and Scripting

regular expressions

Hello, Let say I have a string with content "Free 100%". How can extract only "100" using ksh? I would this machanism to work if instead of "100" there is any kind of combination of numbers(ex. "32", "1238", "1"). I want to get only the digits. I have written something like this: ... (4 Replies)
Discussion started by: whatever
4 Replies

7. UNIX for Dummies Questions & Answers

regular expressions

Hi Gurus, I need help with regular expressions. I want to create a regular expression which will take only alpha-numeric characters for 7 characters long and will throw out an error if longer than that. i tried various combinations but couldn't get it, please help me how to get it guys. ... (2 Replies)
Discussion started by: ragha81
2 Replies

8. Shell Programming and Scripting

Help with regular expressions

I have following content in the file CancelPolicyMultiLingual3=U|PC3|EN RestaurantInfoCode1=U|restID1|1 ..... I am trying to use following matching extression \|(+) to get this PC3|EN restID1|1 Obviously it does not work. Any ideas? (13 Replies)
Discussion started by: arushunter
13 Replies

9. Programming

regular expressions in c++

How do I use the regular expressions in c++? (2 Replies)
Discussion started by: szzz
2 Replies

10. Shell Programming and Scripting

Regular Expressions

I'm trying to parse RichText to XML. I want to be able to capture everything between the '/par' tag in the RTF but not include the tag itself. So far all I have is this, '.*?\\par' but it leaves '\par' at the end of it. Any suggestions? (1 Reply)
Discussion started by: AresMedia
1 Replies
Login or Register to Ask a Question