Re: isolating text in a string



Tim Bowden wrote:
Hi,

Hello,

I'm trying to isolate text in a string. My string is typically of the
form:
sometext["moretext",

I would like to isolate moretext.

I tried:
#!/usr/bin/perl -w
my $string = 'sometext["moretext",';
print $string;
my $snippet;
($snippet, $snippet) = split(/$string/,\[,2);

I assume that you really meant to say:

( $snippet, $snippet ) = split /\[/, $string, 2;

But why are you using the same variable twice in the assignment list? Perhaps you meant this instead:

my ( undef, $snippet ) = split /\[/, $string, 2;


print "$snippet\n";

I don't seem to be able to escape the [ in the split function, so two
questions:
1. How do I escape the [?

See above.

2. What would be a better way to approach this problem?

Is 'sometext[' always the same or variable? Is 'moretext' always enclosed in double quotes and preceded by [ and followed by ,?

Perhaps:

my ( $snippet ) = $string =~ /\w\["([^"]+)",/;



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
.



Relevant Pages

  • Re: problems with opening files due to files path
    ... GUI or it is a console app. ... of what an escape character and escape sequence is. ... character) inside a string specially, it makes the character after the ...
    (comp.lang.python)
  • Re: about escape sequence in RC file
    ... \r can be used as escape sequence in string table of RC ... string resources, embedded quotes don't need to be escaped (the escapes are ... prevent the user from changing the typeface to one that does not. ... place of the escaped character. ...
    (microsoft.public.vc.mfc.docview)
  • Re: Escape character treatment in string library functions
    ... Can I change the escape character used by string libraries?. ... treat '\r' etc special making it difficult to parse even char by ...
    (comp.programming)
  • Re: [PHP] Re: optimilize web page loading
    ... the bytecode is the same. ... heard that the parsing is the same too - because escaped characters can ... Single quoted strings do support some escape characters. ... creating a string via single quotes. ...
    (php.general)
  • Re: RegEx and Vb.net /// "Unrecognized escape sequence"
    ... Not that \ is the escape character for regular expressions. ... VB.Net does not use the \ character as an escape character in strings. ... Dim fileName As String ...
    (microsoft.public.dotnet.framework)