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
  • 836
  • 5
  • 18
  • 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. – Frédéric 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,700
  • 12
  • 14
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
  • 19,046
  • 4
  • 38
  • 71
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
  • 65,661
  • 4
  • 77
  • 114