[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Alexandru Popescu

10/27/2006 1:24:00 PM

Hi!

I have a string in the following form: 2006%2F10%2Fasdfasdf (or more
generic: any_characters+%2Fany_characters+%2Frest_of_it). I am
wondering if I can retrieve the groups 2006, 10, asdfasdf thru a regex
(I confess I couldn't figure it out so far :-( ).

many thanks,

/alex
--
w( the_mindstorm )p.

28 Answers

dblack

10/27/2006 1:28:00 PM

0

Thomas Adam

10/27/2006 1:28:00 PM

0

On Fri, 27 Oct 2006 22:23:42 +0900
"Alexandru Popescu" <the.mindstorm.mailinglist@gmail.com> wrote:

> Hi!
>
> I have a string in the following form: 2006%2F10%2Fasdfasdf (or more
> generic: any_characters+%2Fany_characters+%2Frest_of_it). I am
> wondering if I can retrieve the groups 2006, 10, asdfasdf thru a regex
> (I confess I couldn't figure it out so far :-( ).
>
> many thanks,
>
> ./alex
> --
> .w( the_mindstorm )p.
>

``
[thomas@debian ~]% irb
irb(main):001:0> a="2006%2F10%2Fasdfasdf".split('%2F')
=> ["2006", "10", "asdfasdf"]
irb(main):002:0>
''

-- Thomas Adam

Chris Gernon

10/27/2006 2:13:00 PM

0

Alexandru Popescu wrote:
> I have a string in the following form: 2006%2F10%2Fasdfasdf (or more
> generic: any_characters+%2Fany_characters+%2Frest_of_it). I am
> wondering if I can retrieve the groups 2006, 10, asdfasdf thru a regex

String#split would be easier than using a regex in this case:

irb(main):001:0> '2006%2F10%2Fasdfasdf'.split('%2F')
=> ["2006", "10", "asdfasdf"]

--
Posted via http://www.ruby-....

Alexandru Popescu

10/27/2006 2:24:00 PM

0

On 10/27/06, Chris Gernon <kabigon@gmail.com> wrote:
> Alexandru Popescu wrote:
> > I have a string in the following form: 2006%2F10%2Fasdfasdf (or more
> > generic: any_characters+%2Fany_characters+%2Frest_of_it). I am
> > wondering if I can retrieve the groups 2006, 10, asdfasdf thru a regex
>
> String#split would be easier than using a regex in this case:
>
> irb(main):001:0> '2006%2F10%2Fasdfasdf'.split('%2F')
> => ["2006", "10", "asdfasdf"]
>

Thanks for all suggestions, but the requirement is to be done thru
regex only :-). I knew how to do it with split, but I need to do it
with regexps only.

/alex
--
w( the_mindstorm )p.


> --
> Posted via http://www.ruby-....
>
>

Chris Gernon

10/27/2006 2:39:00 PM

0

Alexandru Popescu wrote:
> Thanks for all suggestions, but the requirement is to be done thru
> regex only :-). I knew how to do it with split, but I need to do it
> with regexps only.

How about just:

irb(main):001:0> match = '2006%2F10%2Fasdfasdf'.match
/^(.*)%2F(.*)%2F(.*)$/
=> #<MatchData:0x1cf404>
irb(main):002:0> match[1]
=> "2006"
irb(main):003:0> match[2]
=> "10"
irb(main):004:0> match[3]
=> "asdfasdf"

--
Posted via http://www.ruby-....

Jon Lim

10/27/2006 2:39:00 PM

0

s = "2006%2F10%2Fasdfasdf"
year,month,other = URL.decode(s).split(/\//)

On 27/10/06, Alexandru Popescu <the.mindstorm.mailinglist@gmail.com> wrote:
> Hi!
>
> I have a string in the following form: 2006%2F10%2Fasdfasdf (or more
> generic: any_characters+%2Fany_characters+%2Frest_of_it). I am
> wondering if I can retrieve the groups 2006, 10, asdfasdf thru a regex
> (I confess I couldn't figure it out so far :-( ).
>
> many thanks,
>
> ./alex
> --
> .w( the_mindstorm )p.
>
>


--
http://www.snowbl...

dblack

10/27/2006 2:40:00 PM

0

Robert Klemme

10/27/2006 2:59:00 PM

0

On 27.10.2006 16:39, dblack@wobblini.net wrote:
> Hi --
>
> On Fri, 27 Oct 2006, Alexandru Popescu wrote:
>
>> On 10/27/06, Chris Gernon <kabigon@gmail.com> wrote:
>>> Alexandru Popescu wrote:
>>> > I have a string in the following form: 2006%2F10%2Fasdfasdf (or more
>>> > generic: any_characters+%2Fany_characters+%2Frest_of_it). I am
>>> > wondering if I can retrieve the groups 2006, 10, asdfasdf thru a regex
>>>
>>> String#split would be easier than using a regex in this case:
>>>
>>> irb(main):001:0> '2006%2F10%2Fasdfasdf'.split('%2F')
>>> => ["2006", "10", "asdfasdf"]
>>>
>>
>> Thanks for all suggestions, but the requirement is to be done thru
>> regex only :-). I knew how to do it with split, but I need to do it
>> with regexps only.
>
> Regexes alone don't do anything other than specify a pattern. You
> need to *use* a regular expression in some operation (like split) to
> get a result.

Actually the code you presented did not even use a RX. You used the
string form of split, didn't you? :-)

Kind regards

robert

Alexandru Popescu

10/27/2006 3:02:00 PM

0

On 10/27/06, dblack@wobblini.net <dblack@wobblini.net> wrote:
> Hi --
>
> On Fri, 27 Oct 2006, Alexandru Popescu wrote:
>
> > On 10/27/06, Chris Gernon <kabigon@gmail.com> wrote:
> >> Alexandru Popescu wrote:
> >> > I have a string in the following form: 2006%2F10%2Fasdfasdf (or more
> >> > generic: any_characters+%2Fany_characters+%2Frest_of_it). I am
> >> > wondering if I can retrieve the groups 2006, 10, asdfasdf thru a regex
> >>
> >> String#split would be easier than using a regex in this case:
> >>
> >> irb(main):001:0> '2006%2F10%2Fasdfasdf'.split('%2F')
> >> => ["2006", "10", "asdfasdf"]
> >>
> >
> > Thanks for all suggestions, but the requirement is to be done thru
> > regex only :-). I knew how to do it with split, but I need to do it
> > with regexps only.
>
> Regexes alone don't do anything other than specify a pattern. You
> need to *use* a regular expression in some operation (like split) to
> get a result.
>

Yes... use groupings, but what I wanted to get is not done through
string.split or something, but through string =~ /pattern/ and than
like in Perl whatever to have access to the groups through $1, $2,
etc.

/alex
--
w( the_mindstorm )p.

>
> David
>
> --
> David A. Black | dblack@wobblini.net
> Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
> DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
> [1] http://www.manning... | [3] http://www.rubypoweran...
> [2] http://dablog.r... | [4] http://www.rubyc...
>
>

Robert Klemme

10/27/2006 3:04:00 PM

0

On 27.10.2006 16:39, Chris Gernon wrote:
> Alexandru Popescu wrote:
>> Thanks for all suggestions, but the requirement is to be done thru
>> regex only :-). I knew how to do it with split, but I need to do it
>> with regexps only.
>
> How about just:
>
> irb(main):001:0> match = '2006%2F10%2Fasdfasdf'.match
> /^(.*)%2F(.*)%2F(.*)$/
> => #<MatchData:0x1cf404>
> irb(main):002:0> match[1]
> => "2006"
> irb(main):003:0> match[2]
> => "10"
> irb(main):004:0> match[3]
> => "asdfasdf"

This has a potential for disastrous backtracking with large strings.
This one is better - if you can guarantee there there is no "%" besides
the one preceding the "2F":

=> "2006%2F10%2Fasdfasdf"
>> s.match(/^([^%]*)%2F([^%]*)%2F(.*)$/).to_a
=> ["2006%2F10%2Fasdfasdf", "2006", "10", "asdfasdf"]

Or maybe even

>> s.match(/^((?>[^%]*))%2F((?>[^%]*))%2F((?>.*))$/).to_a
=> ["2006%2F10%2Fasdfasdf", "2006", "10", "asdfasdf"]

:-)

Kind regards

robert