Perl: Better way to match string within a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: Better way to match string within a string
# 1  
Old 12-22-2008
Perl: Better way to match string within a string

Hi,

I'm trying to get one field out of many as follows:

A string of multiple fields separated with "/" characters:
"/ab=12/cd=34/12=ab/34=cd/ef=pick-this.one/gh=blah/ij=something/"

I want to pick up the field "ef=pick-this.one" which has no regular pattern except it starts with "ef=xxxx" and ends to a "/"

I wrote a little script to get it, but I'm just wondering if there is some better way of doing this(using Perl):

$ ./matchtest.pl
Unwated beginning: /ab=12/cd=34/12=ab/34=cd/
Unwated ending: /gh=blah/ij=something/
Wanted string: pick-this.one

Using this code:
Code:
#!/usr/bin/perl

my($text) = "/ab=12/cd=34/12=ab/34=cd/ef=pick-this.one/gh=blah/ij=something/";
my($prefix,$string,$postfix) = $text =~ m/(.*\/)(ef.*?)(\/.*)$/;
print "Unwated beginning: $prefix\n";
print "Unwated ending: $postfix\n";
$string =~ s/ef=//g;
print "Wanted string: $string\n";

# 2  
Old 12-22-2008
If I understand correctly:

Code:
$ perl -le'
   $s = "/ab=12/cd=34/12=ab/34=cd/ef=pick-this.one/gh=blah/ij=something/";
   print $s =~ m|ef=(.*?)/|;
  '
pick-this.one

# 3  
Old 12-22-2008
the pattern to use is:

Code:
my $text = "/ab=12/cd=34/12=ab/34=cd/ef=pick-this.one/gh=blah/ij=something/";
my ($ef) = $text =~ m|/ef=([^/]+)/|;
print $ef;

# 4  
Old 01-11-2009
Late thanks for your replies guys Smilie works fine!

//Juha
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove not only the duplicate string but also the keyword of the string in Perl

Hi Perl users, I have another problem with text processing in Perl. I have a file below: Linux Unix Linux Windows SUN MACOS SUN SUN HP-AUX I want the result below: Unix Windows SUN MACOS HP-AUX so the duplicate string will be removed and also the keyword of the string on... (2 Replies)
Discussion started by: askari
2 Replies

2. Shell Programming and Scripting

String match, with perl command

cat clinvar_00-latest.vcf | perl -aF/\\t/ -lne '/CLNSRCID=(\d+)/ and print join("\t",@F,$1)' > OMIM.txt The above code finds the text CLNSRCID=, but only outputs those records in which there is a numerical value only. For example, the first match is CLNSRCID=103320.0001 in line 4 of the... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

awk : match the string and string with the quotes :

Hi all, Here is the data file: - want to match only lan3 in the output . - not lan3:1 file : OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE=""... (2 Replies)
Discussion started by: rveri
2 Replies

4. Shell Programming and Scripting

perl regex string match issue..kindly help

i have a script in which i need to skip comments, and i am able to achieve it partially... IN text file: {**************************** {test : test...test } Script: while (<$fh>) { push ( @data, $_); } if ( $data =~ m/(^{\*+$)/ ){ } With the above match i am... (5 Replies)
Discussion started by: avskrm
5 Replies

5. Shell Programming and Scripting

Perl : how to match non-empty string that has no spaces

Hi Everyone, I am looking for neat way to grep a non-empty string that basically contains a hostname, which might be in FWDN form or without the domain, for example: hostname.internal.domainname.net The file I am parsing contains blan lines (^$) and also series of "-" which in other places... (2 Replies)
Discussion started by: togr
2 Replies

6. Shell Programming and Scripting

Match hex value in string (Perl)

I am trying to match a character return from a website so that I can replace it. It is the '...' character (didnt even know it existed initially). The character apparently has the hex value of 2026, but in the script, attempting to substitute regular 3 periods is not working. What am I... (2 Replies)
Discussion started by: Guyverix
2 Replies

7. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

8. Shell Programming and Scripting

perl get partial string of a string

Hi All, I have: $d = "12.02222222222"; $d =~ s/(.*).(.*)/$1/e; The output should be just 12. Please guide me my expression wrong. Thanks (5 Replies)
Discussion started by: jimmy_y
5 Replies

9. Shell Programming and Scripting

pattern match url in string / PERL

Am trying to remove urls from text strings in PERL. I have the following but it does not seem to work: $remarks =~ s/www\.\s+\.com//gi; In English, I want to look for www. then I want to delete the www. and everything after it until I hit a space (but not including the space). It's not... (2 Replies)
Discussion started by: mrealty
2 Replies

10. Shell Programming and Scripting

[Perl] Find one string, change another string.

Hi, In principle I am searching for a Perl equivalent for this sed command: sed "/TIM_AM_ARGS=/ s/60/1440/" $EDIT_FILE > $TEMP_FILE cp $TEMP_FILE $EDIT_FILE I was wondering if it needs to be like this, or that there other, shorter, alternatives: open (TIMENVFILE, "<$timenvfile") or die... (5 Replies)
Discussion started by: ejdv
5 Replies
Login or Register to Ask a Question