Simple question

B

Barty Slartfast

I have a string of which the middle part is an unknown generated by user
input, for example:

ladiddfa BEGIN balblabla END and whatever else..."

The middle part being --> balblabla <---

I would like to extract whatever string is within the BEGIN and END
delimiters, but I don't know in advance what the middle part is.
It may be any number of characters, numbers, empty spaces, linebreaks, or
nothing at all.
Any idea of the simplest possible perl code to do this?
 
W

Walter Roberson

:I have a string of which the middle part is an unknown generated by user
:input, for example:

:ladiddfa BEGIN balblabla END and whatever else..."

:The middle part being --> balblabla <---

:I would like to extract whatever string is within the BEGIN and END
:delimiters, but I don't know in advance what the middle part is.
:It may be any number of characters, numbers, empty spaces, linebreaks, or
:nothing at all.
:Any idea of the simplest possible perl code to do this?

if ( m/BEGIN\s(.*?)\sEND/s ) {
$middle = $1;
} else {
???
}
 
T

Tore Aursand

if ( m/BEGIN\s(.*?)\sEND/s ) {
$middle = $1;
} else {
???
}

Better yet, IMO;

if ( m,BEGIN\s*(.*?)\s*END,s ) {
my $middle = $1;
}
else {
# No match
}

Just from the top of my head, but I'm sure that the OP don't want any
extra spaces. :)
 
W

Walter Roberson

:Better yet, IMO;

: if ( m,BEGIN\s*(.*?)\s*END,s ) {
: my $middle = $1;
: }
: else {
: # No match
: }

but $middle is going to disappear after that 'if', which is not necessarily
to be desired.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top