Salomanuel
Salomanuel

Reputation: 955

select quotes but NOT apostrophes in REGEX

I would like to achieve this amazing result (I'm using Ruby):

input:  "Joe can't tell between 'large' and large."   
output: "Joe can't tell between large and large."

getting rid of the quotes but not of the apostrophe
how can I do it in a simple way?

my failed overcomplicated attempt:

 entry = test[0].gsub(/[[']*1]/, "")

Upvotes: 0

Views: 880

Answers (4)

Steve Midgley
Steve Midgley

Reputation: 2344

Here's a script to demo an answer:

x = "Joe can't tell between 'large' and large."
puts x.gsub(/'\s|\s'/, " ")
# Output: Joe can't tell between large and large.

To decode what this script does - the gsub / regex line is saying:

Find all (an apostrophe followed by a space '/s) or (a space followed by an apostrophe \s') and replace it with space.

This leaves apostrophes that aren't adjacent to spaces intact, which seems to remove only the apostrophes the OP is trying to remove.

Upvotes: 2

IanGabes
IanGabes

Reputation: 2797

This does exactly what you are looking for, including ignoring the posted comments Students' example.

entry = test[0].gsub(/'(­[^\s]+)'/,­ '\1')

I don't have ruby set up, but i confirmed this works here: http://tryruby.org/levels/1/challenges/0

Here is an example on regex101:

https://regex101.com/r/aY8aJ3/1

Upvotes: 0

user2705585
user2705585

Reputation:

Simplest one for your situation could be something like this.

Regex: /\s'|'\s/ and replace with a space.

Regex101 Demo


You can also go with /(['"])([A-Za-z]+)\1/ and replace with \2 i.e second captured group.

Regex101 Demo

Upvotes: 3

Dimitar
Dimitar

Reputation: 4801

Maybe this one?

entry = test[0].gsub(/[^']/, "")

But it should remove all '.

Upvotes: 0

Related Questions