2

We have a free text:

sal{del{rf}ghladfs}wds{w12rf}qq  

Output should be:

salwdsqq

Please share various approaches if possible. For example : lapply, gsub, for/while loop, grep

Navin Manaswi
  • 964
  • 7
  • 19
  • 3
    Please [edit] the question and tell what the relation is between your input and desired output. I can guess, but it's up to you to write good questions. The less people have to puzzle what you mean, the better your chances of getting an answer. – Jan Doggen May 29 '15 at 14:45
  • 2
    Which language are you using ? This is very vague. – Frederic Adda May 29 '15 at 14:47
  • 7
    Are you missing one opening `{` or is the text correct as is? – talat May 29 '15 at 14:47
  • 1
    @FredA., probably R since it's tagged with it – talat May 29 '15 at 14:48
  • 1
    Related: http://stackoverflow.com/q/546433/1191259 – Frank May 29 '15 at 14:52
  • I don't think a regex in R is going to help unless it supports recursion for matching balanced text. You could do your own functional recursion though since you only have two types `{}` and alnum's. –  May 29 '15 at 16:10

3 Answers3

1

This works in R

string1 <- "sal{del{rf}ghla}dfs}wds{w12rf}qq"
string2 <- gsub("{[^{}}]*}", "", string1, perl = TRUE)
string3 <- gsub("{.*}", "", string2, perl = TRUE)
string3
Michele Usuelli
  • 1,970
  • 13
  • 15
  • I think you have an extra closing bracket in `[^{}}]` – Frank May 29 '15 at 15:03
  • plz see the edited question. Sorry for inconvenience – Navin Manaswi May 29 '15 at 15:10
  • This answer also works fine with balanced brackets, like `s <- "sal{{del{rf}ghla}dfs}wds{w12rf}qq"` and in this case has a recursive extension: `s2 <- s; while(grepl("{[^{}]*}", s2, perl = TRUE)) s2 <- gsub("{[^{}]*}", "", s2, perl = TRUE)` @user3142384 Also, it seems to still work fine after your change to the question... – Frank May 29 '15 at 15:11
1

I think this would work whether you had balanced or unbalanced brackets:

unbalanced (as in the q)

x <- "sal{del{rf}ghla}dfs}wds{w12rf}qq  "
paste0(gsub('\\w+}|[{} ]', '', strsplit(x, '\\{\\w+')[[1]]), collapse = '')
# [1] "salwdsqq"

inserted one randomly

x <- "sal{del{{rf}ghla}dfs}wds{w12rf}qq  "
paste0(gsub('\\w+}|[{} ]', '', strsplit(x, '\\{\\w+')[[1]]), collapse = '')
# [1] "salwdsqq"
rawr
  • 20,481
  • 4
  • 44
  • 78
1

You can do this using a recursive regular expression.

x <- 'sal{del{rf}{sfddfdffdf}ghladfs}wds{w12rf}qq'
gsub('{(?:[^{}]+|(?R))*+}', '', x, perl=TRUE)
# [1] "salwdsqq"
hwnd
  • 69,796
  • 4
  • 95
  • 132