wyc
wyc

Reputation: 55273

Using regex to remove everything after "..."

I have a string like this:

"Product DescriptionThe Signature Series treatment makes the strategy guide a COLLECTIBLE ITEM for StarCraft II fans. Single-player CAMPAIGN WALKTHROUGH covers all possible mission branches, including bonus objectives throughout the campaign. Exclusive MAPS found only in the official guide, show locations of units,... \n\n\n\n\n\n\nRead More\n"

How can I use regex the .text[/(...)/] to remove what's after the ... so that it outputs like below?

"Product DescriptionThe Signature Series treatment makes the strategy guide a COLLECTIBLE ITEM for StarCraft II fans. Single-player CAMPAIGN WALKTHROUGH covers all possible mission branches, including bonus objectives throughout the campaign. Exclusive MAPS found only in the official guide, show locations of units,..."

Upvotes: 1

Views: 904

Answers (4)

Sajan Chandran
Sajan Chandran

Reputation: 11487

You can also do

/(^.*)(\.\­.\..*)/.ma­tch(string).captur­es[0]

Upvotes: 0

Jerry
Jerry

Reputation: 71538

You should be able to use the split method, something like that (I'm not that familiar with Ruby).

arr = text.split("...")  #=> text before '...' and text after '...'
arr[0]                   #=> text before '...'

Upvotes: 2

Qtax
Qtax

Reputation: 33908

No need to use lookarounds, you could simply do something like:

string.sub(/\.{3}.*/s, "...")

Upvotes: 1

sawa
sawa

Reputation: 168101

You need to escape the period.

string.sub(/(?<=\.\.\.).*/m, "")

or

string[/.*\.\.\./m]

Upvotes: 3

Related Questions