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;
Printable View
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;
Use a regular expression.
I hope I got string begin and end right (^ and $ I think, but I'm not sure).Code:$input =~ /^(.+)\?(.+):(.+);$/
// $1, $2 and $3 hold the parts
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! :)
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?
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
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.
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?
$html =~ s/(\d)\?(.+?):(.+?);/$1 ? $2 : $3/egs;
You forgot to escape the ?
Thanks alot!!!
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;
Exactly.
Except that the "all" is "one or more of any character, use the least possible"
So . is all char
+ is one or more, and ? is what
. 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.
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 =)
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!
$html =~ s/(\d?)\?(.+?):(.+?);/$1 ? $2 : $3/egs;
The ? here means "0 or 1 times".
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?
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.