1

I need to clean some portion text like:

  1. "P{ MARGIN: 0px } Le Lorem Ipsum est simplement du faux texte employé dans la composition et la mise en page avant impression."

  2. "BODY{ MARGIN: 0px } Le Lorem Ipsum est simplement du faux texte employé dans la composition et la mise en page avant impression."

do you have some regex for me? Thanks in advance.

Note: For .Net desktop application using System.Text.RegularExpressions.

Thanks in advance.

2
  • you need it for when? Commented May 31, 2013 at 20:38
  • What does Extract x{} from some text mean?
    – paparazzo
    Commented May 31, 2013 at 20:46

1 Answer 1

2

Your requirements are pretty vague, but this pattern,

\w+\s*{.*?}

Will match one or more of word characters followed by an open bracket, followed by any number of whitespace characters, followed by any number of characters (non-greedily), followed by a close bracket.

For example:

string input = "P { MARGIN: 0px } Lorem Ipsum";
string output = Regex.Match(input, @"\w+\s*{.*?}").Value;
System.Console.WriteLine(output); // P { MARGIN: 0px }
6
  • {} are regex special characters, remember to escape them
    – Isaac
    Commented May 31, 2013 at 20:47
  • @Isaac - being that the braces don't contain something like {3} or {3,4}, I don't think it is strictly necessary to escape them (java.util.regex being a notable exception to that, in which escaping would be required).
    – femtoRgon
    Commented May 31, 2013 at 20:50
  • @Isaac It may be different in other Regex engines, but at least in .NET's, it appears you only need to escape {} when it can be interpreted as a quantifier (e.g. {2,}).
    – p.s.w.g
    Commented May 31, 2013 at 20:51
  • Thanks @p.s.w.g but the regex does not work in case: "P { MARGIN: 0px } Lorem Ipsum" Commented May 31, 2013 at 21:02
  • "P { 30spaces MARGIN: 0px } Lorem Ipsum" Commented May 31, 2013 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.