Java String Split by “|”

You must use: String [] temp = s.split(“\\|”); This is because the split method takes a regular expression, and | is one of the special characters. It means ‘or’. That means you are splitting by ” or ”, which is just ”. Therefore it will split between every character. You need two slashes because the … Read more

Remove characters before and including _ in python 2.7

To get all text on a line after a underscore character, split on the first _ character and take the last element of the result: line.split(‘_’, 1)[-1] This will also work for lines that do not have an underscore character on the line. Demo: >>> ‘Grp25_QTY47 5’.split(‘_’, 1)[-1] ‘QTY47 5’ >>> ‘No underscore’.split(‘_’, 1)[-1] ‘No … Read more

Lua need to split at comma

Try this str=”cat,dog” for word in string.gmatch(str, ‘([^,]+)’) do print(word) end ‘[^,]’ means “everything but the comma, the + sign means “one or more characters”. The parenthesis create a capture (not really needed in this case).

javascript split string by space, but ignore space in quotes (notice not to split by the colon too)

s=”Time:”Last 7 Days” Time:”Last 30 Days”” s.match(/(?:[^\s”]+|”[^”]*”)+/g) // -> [‘Time:”Last 7 Days”‘, ‘Time:”Last 30 Days”‘] Explained: (?: # non-capturing group [^\s”]+ # anything that’s not a space or a double-quote | # or… ” # opening double-quote [^”]* # …followed by zero or more chacacters that are not a double-quote ” # …closing double-quote )+ … Read more

How to keep the delimiters of Regex.Split?

Just put the pattern into a capture-group, and the matches will also be included in the result. string[] result = Regex.Split(“123.456.789″, @”(\.)”); Result: { “123”, “.”, “456”, “.”, “789” } This also works for many other languages: JavaScript: “123.456.789”.split(/(\.)/g) Python: re.split(r”(\.)”, “123.456.789”) Perl: split(/(\.)/g, “123.456.789”) (Not Java though)

How to split string across new lines and keep blank lines?

I’d recommend using lines instead of split for this task. lines will retain the trailing line-break, which allows you to see the desired empty-line. Use chomp to clean up: “aaaa\nbbbb\n\n”.lines.map(&:chomp) [ [0] “aaaa”, [1] “bbbb”, [2] “” ] Other, more convoluted, ways of getting there are: “aaaa\nbbbb\n\n”.split(/(\n)/).each_slice(2).map{ |ary| ary.join.chomp } [ [0] “aaaa”, [1] “bbbb”, … Read more