|
-
Jan 14th, 2004, 06:05 PM
#1
Thread Starter
Frenzied Member
perl: Split a string?
Im stuck trying to split this string
It must split all of my examples. I want to seperate them by the character by these three characters
?:;
1?Not Insured:Insured;
1?No:Yes;
1?option1:option2;
-
Jan 15th, 2004, 03:55 AM
#2
Use a regular expression.
Code:
$input =~ /^(.+)\?(.+):(.+);$/
// $1, $2 and $3 hold the parts
I hope I got string begin and end right (^ and $ I think, but I'm not sure).
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 15th, 2004, 10:33 AM
#3
Thread Starter
Frenzied Member
You have been some help, but that code doesnt do what I need. So let me try again.
Say I have a html page in a scalar. $html
in this page could be the string 1?Not Insured:Insured;
So I want to go threw the HTML and loop threw and put all of these expressions into 3 strings
$inp1 = 1;
$inp2 = "Not Insured";
$inp3 = "Insured";
Can you help me? Thanks so much!
-
Jan 15th, 2004, 11:27 AM
#4
What does $html contain? Some random HTML code, with this stuff embedded? Or only such triples? What more do we know about these triples except that they are separated by ? and : and finished by ;? Are they all on their own line? Is the first part always a number?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 15th, 2004, 11:54 AM
#5
Thread Starter
Frenzied Member
Its an entire html page, and these are embedded in this page.
They ALWAYS are in this format
number?option1:option2;
number is going to be which option will show up, and im gonna erase the rest of the string in the html. Get it?
So if its 1?No:Yes; it will say Yes
and 0?No:Yes; will say No
-
Jan 15th, 2004, 12:18 PM
#6
Hmmm....
There's something with execution in the second part of search-replace regular expressions that should do this nearly by itself. But I'm no expert in perl.
It's something like
$html =~ s/(\d)?(.+?):(.+?);/$1 ? $2 : $3/egs;
I believe this should work.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 15th, 2004, 01:40 PM
#7
Thread Starter
Frenzied Member
Wow!
It ate my html up like roast beef, and them spit out stuff all over the place. Funny but kinda sad!
Heh.. Any idea why it would do that?
-
Jan 15th, 2004, 01:45 PM
#8
Thread Starter
Frenzied Member
$html =~ s/(\d)\?(.+?):(.+?);/$1 ? $2 : $3/egs;
You forgot to escape the ?
Thanks alot!!!
-
Jan 15th, 2004, 01:52 PM
#9
Thread Starter
Frenzied Member
I made a note to my self of how you managed to do that with that regex.
Is my understanding correct?
#NOTE: This line swaps out expressions in the html that
#look like this
#num?option1?option2
#if num = 1 then uses option2, else option1
#switch / var1(digit) escape? var2(all) : var3(all) ; / if 1 then 3, else 2 / right=expression, get all, single line input
$html =~ s/(\d)\?(.+?):(.+?);/$1 ? $2 : $3/egs;
-
Jan 15th, 2004, 03:54 PM
#10
Exactly.
Except that the "all" is "one or more of any character, use the least possible"
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 15th, 2004, 07:09 PM
#11
Thread Starter
Frenzied Member
So . is all char
+ is one or more, and ? is what
-
Jan 16th, 2004, 08:15 AM
#12
. means any char, + means one or more, the ? makes + non-greedy.
Consider this string:
blabla&foobar&useless
and this regular expression
(.+)&(.+)
Any character, 1 or more times, followed by &, followed by any character 1 or more times.
Now, there are two ways to match this. Way #1:
$1 = blabla&foobar
$2 = useless
Way #2:
$1 = blabla
$2 = foobar&useless
Which should apply? The standard behavior is to be greedy, that is, to match as much as possible while still matching the expression as a whole. This means that the first .+ matches everything up to the second &, which is more than matching up to the first &, so Way #1 will be chosen by default,
The ? after the + makes it non-greedy, so that it matches as little as possible. Way #2 will be chosen for the regular expression
(.+?)&(.+)
The reason why I have these in your case is simple. If your $html looks like
Some characters1?one:other;some more characters0?other:one;and some final characters.
Then a greedy match would grab
$1 = 1
$2 = one:other;some more characters0?other
$3 = one
Which is not what you want.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 16th, 2004, 10:05 AM
#13
Thread Starter
Frenzied Member
oh, that makes so much more sense now.
I wondered what 'greedy' was.
thanks alot for the help. im sure I will be asking
you lots more later =)
-
Jan 16th, 2004, 04:19 PM
#14
Thread Starter
Frenzied Member
problem
Sometimes there is no number, and it doesnt get the string.
So is it possible if there is no number to act like its 0 ???
how would you code that?
i tried this:
$html =~ s/(\d|)\?(.+?):(.+?);/$1 ? $2 : $3/egs;
the pipe didnt help.
Thanks in advance!
-
Jan 17th, 2004, 03:43 AM
#15
$html =~ s/(\d?)\?(.+?):(.+?);/$1 ? $2 : $3/egs;
The ? here means "0 or 1 times".
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 17th, 2004, 02:32 PM
#16
Thread Starter
Frenzied Member
so where did you learn the regex so well, I do for work
everyday ,so I want to learn it well.
Whats the best way?
-
Jan 18th, 2004, 06:34 AM
#17
No idea actually. I read through a few tutorials and simply used them.
I found the JavaScript Regex reference by Netscape to be quite good, or the perl reference (manpages).
If you've got some *nix system, just type man perl and check which chapter you need.
On Windows, there might have come a reference with your Perl installation.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|