Home > Mobile >  Regular Expressions to select all the text that is “not”: Curly brackets {} and whats between them?
Regular Expressions to select all the text that is “not”: Curly brackets {} and whats between them?

Time:01-23

if I have a text like this:

Lorem ipsum dolor amet, consectetur {adipiscing elit bghy}. Ftoam sem augue {tincidunt sit amet}, volutpat vel, egestas quis {lacus gonec mattis}. nisi et lacinia vehicula, lectus mi {luctus urna eu molestie diam lectus vel eros}. Donec a massa neque.

What is the Regular Expressions to select all the text that is “not”: Curly brackets {} and whats between them?

This Regular Expressions select Curly brackets {} and what is between them. What I want is: all the text that is “not”: Curly brackets {} and whats between them.

\{[^}]*\}

CodePudding user response:

You did not tag with any tool or prgramming language, but you did tag with regex replacement. Assuming you can do a regex replacement, use the following find and replace:

Find:     \s*\{.*?\}\s*
Replace: (single space)

The regex replacement strategy is to remove all terms in curly braces. Note that you might also need to trim whitespace from the start/end, as the above replacement could leave dangling whitespace there.

Here is a working regex demo.

If you can't do a replacement, you could try finding on the following regex pattern:

(?<=[^\S{])\w [,;:.]?(?: \w [,;:.]?(?=[^\S}]))*

This matches one or more terms, possibly separated by punctuation. It insists that the first and last term are not surrounded by {...}.

CodePudding user response:

Using a negative lookahead and possessive quantifier:

[^}{]  (?!\})

Here is the demo at regex101

  • Related