regular expression to extract a string between 2 other strings 
Author Message
 regular expression to extract a string between 2 other strings

I am new to regular expressions and would really appreciate some
pointers in the right direction - I have searched google and can't get
the answer to the following problem

I want to extract a string from a file which is always surrounded by
the same text, eg.:
example.txt contains:
====================================================
lots of text

This is the identifier (STRING) lots of other text"
====================================================

I would like to extract STRING from example.txt. In all files like
example.txt STRING is always directly preceded by "This is the
identifier (" and directly followed by ")".

I would be grateful if someone could tell me the neatest way to
extract this.
TIA
Redge.



Mon, 28 Mar 2005 09:43:01 GMT  
 regular expression to extract a string between 2 other strings

Quote:

> I am new to regular expressions and would really appreciate some
> pointers in the right direction - I have searched google and can't get
> the answer to the following problem

> I want to extract a string from a file which is always surrounded by
> the same text, eg.:
> example.txt contains:
>====================================================
> lots of text

> This is the identifier (STRING) lots of other text"
>====================================================

> I would like to extract STRING from example.txt. In all files like
> example.txt STRING is always directly preceded by "This is the
> identifier (" and directly followed by ")".

> I would be grateful if someone could tell me the neatest way to
> extract this.

-------------------------------------
#!/usr/bin/perl
use warnings;
use strict;

$_ = 'This is the identifier (STRING) lots of other text"';

print $1 if /This is the identifier \(([^)]+)\)/;
-------------------------------------

Cheers,
Bernard
--
echo 42|perl -pe '$#="Just another Perl hacker,"'



Mon, 28 Mar 2005 09:53:23 GMT  
 regular expression to extract a string between 2 other strings

| I want to extract a string from a file which is always surrounded by
| the same text, eg.:
| example.txt contains:
| ====================================================
| lots of text
|
| This is the identifier (STRING) lots of other text"
| ====================================================
|
| I would like to extract STRING from example.txt. In all files like
| example.txt STRING is always directly preceded by "This is the
| identifier (" and directly followed by ")".

Use \Q...\E to escape whatever regex metachars are in the
leader/follower strings.

    #!perl
    use warnings;
    use strict;
    my $lead = 'This is the identifier (';
    my $follow = ')';
    my $rx = qr/\Q$lead\E(.+?)\Q$follow\E/;
    my $string = 'This is the identifier (STRING) lots of other text';
    print $1 if $string =~ /$rx/;



Mon, 28 Mar 2005 10:12:56 GMT  
 regular expression to extract a string between 2 other strings
Thanks very much Bernard and Jay - I really appreciate your help.
Redge.
Quote:


> | I want to extract a string from a file which is always surrounded by
> | the same text, eg.:
> | example.txt contains:
> | ====================================================
> | lots of text
> |
> | This is the identifier (STRING) lots of other text"
> | ====================================================
> |
> | I would like to extract STRING from example.txt. In all files like
> | example.txt STRING is always directly preceded by "This is the
> | identifier (" and directly followed by ")".

> Use \Q...\E to escape whatever regex metachars are in the
> leader/follower strings.

>     #!perl
>     use warnings;
>     use strict;
>     my $lead = 'This is the identifier (';
>     my $follow = ')';
>     my $rx = qr/\Q$lead\E(.+?)\Q$follow\E/;
>     my $string = 'This is the identifier (STRING) lots of other text';
>     print $1 if $string =~ /$rx/;



Mon, 28 Mar 2005 22:11:46 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Extracting Strings Matching a Regular Expression

2. regex expression to extract data from a string

3. extract string from another string

4. Extract string from a string

5. regular expression - cutting leading zeros from a string

6. regular expression replace string to pad leading zeros

7. regular expression-search odd number or even number of characters in a string

8. question on string manipulation/regular expressions

9. regular expression function to find multiple strings

10. Using regular expression to zero fill a string?

11. String manipulation using regular expression

12. Regular expression whole string matching

 

 
Powered by phpBB® Forum Software