开发者

Allow alphanumeric and #, replace comma and separate on space

开发者 https://www.devze.com 2023-02-22 03:17 出处:网络
I want to do the following in a regex: 1. allow alphanumeric characters 2. allow the # character, and comma \',\'

I want to do the following in a regex:

1. allow alphanumeric characters
2. allow the # character, and comma ','
3. replace 开发者_StackOverflow社区the comma ',' with a space
4. split on space


sentence = "cool, fun, house234"

>> [cool, fun, house234]


This is a simple way to do it:

sentence.scan(/[a-z0-9#]+/i) #=> ["cool", "fun", "house234"]

Basically it's looking for character runs that contain a to z in upper and lower case, plus 0 to 9, and #, and returning those. Because comma and space aren't matching they're ignored.

You don't show an example using # but I added it 'cuz you said so.


You can do 1 and 2 with a regular expression, but not 3 and 4.

sentence = "cool, fun, house234"

sentence.gsub(',', ' ').split if sentence =~ /[0-9#,]/

=> [ "cool", "fun", "house234" ]


"cool, fun, house234".split(",")
 => ["cool", " fun", " house234"] 

You can just pass the "," into the split method to split on the comma, no need to convert it to spaces.


Probably, what you wanted is this?

string.gsub(/[^\w, #]/, '').split(/ +,? +/)
0

精彩评论

暂无评论...
验证码 换一张
取 消