Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string by non-alphabetic characters

I want to split a string with all non-alphabetic characters as delimiters.

For example, I want to split this string

"hello1 twenty-three / nine.bye"

into

["hello","","twenty","three","","","nine","bye"]

I've tried this

text.split(/\[A-Za-z]+/)

but it isn't working.

How do I split a string by non-alphabetic characters?

like image 847
Peter Olson Avatar asked Mar 23 '12 16:03

Peter Olson


People also ask

How do you split a string with special characters in Python?

Use the re. split() method to split a string on all special characters. The re. split() method takes a pattern and a string and splits the string on each occurrence of the pattern.

How do you extract or omit any non-alphanumeric characters using split that is provided in Java?

A common solution to remove all non-alphanumeric characters from a String is with regular expressions. The idea is to use the regular expression [^A-Za-z0-9] to retain only alphanumeric characters in the string. You can also use [^\w] regular expression, which is equivalent to [^a-zA-Z_0-9] .

How do you separate the characters in a string?

It's as simple as: s. split(""); The delimiter is an empty string, hence it will break up between each single character.


2 Answers

It sounds like you're looking for the not a match atom: [^. Try the following

text.split(/[^A-Za-z]/)
like image 68
JaredPar Avatar answered Oct 19 '22 22:10

JaredPar


Isn't the inital backslash breaking your []? What about text.split(/[^A-Za-z]+/)?

"asdsd22sdsdd".split(/[^A-Za-z]/)
["asdsd", "", "sdsdd"]
like image 43
Jamund Ferguson Avatar answered Oct 19 '22 23:10

Jamund Ferguson