PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Question about split method
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Question about split method

Réponse
 
LinkBack Outils de la discussion
Vieux 25/02/2008, 10h36   #1
Glenn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Question about split method

[Note: parts of this message were removed to make it a legal post.]

Hello,

I'm wondering if there is a method for the String class that splits a string on some characters and keeps the split characters in the elements of the resulting array?

The split method returns an array in this example:

p "This is a sentence. This is a sentence! This is a sentence?".strip.split(/\.|\?|\!/)


["This is a sentence", " This is a sentence", " This is a sentence"]

The three sentences in the above string have very different meanings, but loose those meanings without the punctuation, so I'd like to keep the punctuation. I'd like a method that keeps the split characters, and returns this array:

["This is a sentence.", " This is a sentence!", " This is a sentence?"]

Does such an array exist? If not, would it be possible to modify the split method to produce that result?

I'm running Ruby 1.8.6 on Windows.

Thanks for your .


  Réponse avec citation
Vieux 25/02/2008, 11h07   #2
ThoML
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question about split method

> Does such an array exist? If not, would it be possible to modify the split method to produce that result?

If you put the pattern in a group, it will be included in the array --
but not quite in the way you described:

irb(main):001:0> p "This is a sentence. This is a sentence! This is a
sentence?".strip.split(/(\.|\?|\!)/)
["This is a sentence", ".", " This is a sentence", "!", " This is a
sentence", "?"]

Regards,
Thomas.
  Réponse avec citation
Vieux 25/02/2008, 12h43   #3
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question about split method

2008/2/25, Glenn <glenn_ritz@yahoo.com>:
> Hello,
>
> I'm wondering if there is a method for the String class that splits a string on some characters and keeps the split characters in the elements of the resulting array?
>
> The split method returns an array in this example:
>
> p "This is a sentence. This is a sentence! This is a sentence?".strip.split(/\.|\?|\!/)
>
>
> ["This is a sentence", " This is a sentence", " This is a sentence"]
>
> The three sentences in the above string have very different meanings, but loose those meanings without the punctuation, so I'd like to keep the punctuation. I'd like a method that keeps the split characters, and returns this array:
>
> ["This is a sentence.", " This is a sentence!", " This is a sentence?"]
>
> Does such an array exist? If not, would it be possible to modify the split method to produce that result?
>
> I'm running Ruby 1.8.6 on Windows.


Hm, you could do it with lookbehind on 1.9. On 1.8 you only have
lookforward which gives you this:

irb(main):002:0> "a. b.".split /(?=\.\s+)/
=> ["a", ". b."]

Not quite what you wanted. :-)

But here's an alternative approach which works with 1.8:

irb(main):005:0> "a. b. c! d? e.".scan /.*?[.!?](?:\s|$)/
=> ["a. ", "b. ", "c! ", "d? ", "e."]

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

  Réponse avec citation
Vieux 25/02/2008, 20h59   #4
James Gray
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Question about split method

On Feb 25, 2008, at 6:43 AM, Robert Klemme wrote:

> 2008/2/25, Glenn <glenn_ritz@yahoo.com>:
>> Hello,
>>
>> I'm wondering if there is a method for the String class that splits
>> a string on some characters and keeps the split characters in the
>> elements of the resulting array?
>>
>> The split method returns an array in this example:
>>
>> p "This is a sentence. This is a sentence! This is a
>> sentence?".strip.split(/\.|\?|\!/)
>>
>>
>> ["This is a sentence", " This is a sentence", " This is a sentence"]
>>
>> The three sentences in the above string have very different
>> meanings, but loose those meanings without the punctuation, so I'd
>> like to keep the punctuation. I'd like a method that keeps the
>> split characters, and returns this array:
>>
>> ["This is a sentence.", " This is a sentence!", " This is a
>> sentence?"]
>>
>> Does such an array exist? If not, would it be possible to modify
>> the split method to produce that result?
>>
>> I'm running Ruby 1.8.6 on Windows.

>
> Hm, you could do it with lookbehind on 1.9. On 1.8 you only have
> lookforward which gives you this:
>
> irb(main):002:0> "a. b.".split /(?=\.\s+)/
> => ["a", ". b."]
>
> Not quite what you wanted. :-)


We can turn look-ahead into into look-behind, though it's not pretty:

$ ruby -ve 'p "This is a sentence. This is a sentence! This is a
sentence?".reverse.split(/(?=(?:\A|\s+)[.!?])/).map { |s|
s.reverse }.reverse'
ruby 1.8.6 (2007-09-24 patchlevel 111) [i686-darwin9.1.0]
["This is a sentence. ", "This is a sentence! ", "This is a sentence?"]

James Edward Gray II

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h38.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12305 seconds with 12 queries