Question

I have a string mixed with number, brackets and characters. I want to split them into different rows. What would be a possible way to do it? Thanks for your help.

For instance, the string is like "3. abc4(). 3. efg3() a() 4.ed.". Final results would look like the following:

3. abc4().
3. efg 3() a()
4. ed.
Was it helpful?

Solution

You could do this with a regular expression. Here's one such way

x <- "3. abc4(). 3. efg3() a() 4.ed."
m <- gregexpr("\\d\\..*?(?=\\s*\\d\\.|$)",x, perl=T)
v <- regmatches(x,m)[[1]]
v; #[1] "3. abc4()."    "3. efg3() a()" "4.ed."

So that gives a vector with each separate element. Then if you want you can combine them with a newline in between and then print that out (using cat otherwise the newlines print as "\n")

cat(paste(v, collapse="\n"))
# 3. abc4().
# 3. efg3() a()
# 4.ed.

If you are new to regular expressions, it may look a bit crazy, but it looks for a single digit, followed by a decimal. Then it does a non greedy match, stopping when what follows is either just spaces then another digit-period combo or the end of the string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top