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: Unrecognized escape sequences in string literals
    ... If you don't know what your string literals are, ... Adding escape codes into the string literal doesn't change this ... extra effort required to defeat the compiler (forcing the programmer to ... And if you saw that in Python, you'd also know that there are some ...
    (comp.lang.python)
  • Re: Unrecognized escape sequences in string literals
    ... need worry that I've misinterpreted what a string literal means. ... You can't expect the compiler to save you from ... Adding escape codes into the string literal doesn't ... (This behavior is useful when debugging: if an escape sequence is ...
    (comp.lang.python)
  • 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: Unrecognized escape sequences in string literals
    ... if he could just look at the string literal and know. ... friend is a programmer. ... If you don't know what a backslash escape is going to do, ... That's an enormous difference from Python, ...
    (comp.lang.python)
  • Re: more on unescaping escapes
    ... without the quotes in the file so my parser can read it as a single ... string. ... It really is a tab that gets stored there, not the escape for one. ... if you give python an unknown escape it passes it leaves it ...
    (comp.lang.python)