Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Regexp in Java to escape double quotes and special cases.

Last post 04-15-2011, 5:39 PM by Misha79. 0 replies.
Sort Posts: Previous Next
  •  04-15-2011, 5:39 PM 80714

    Regexp in Java to escape double quotes and special cases.

    I  have a regex in Java to extract out  double-quoted text .

    Regex in Java : (\"\"|\"[\\s\\S]*?[^\\\\]\") 

    Input can be any String - .

    But there can be cases where strings are : like  

     

     

    p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Monaco}span.s1 {text-decoration: underline}

    private Regex path = new Regex(@"(?<!\S)(?<path>(http://|https://|hg://).+?)(\s+hg|\s*\-\-|[\""\t\r\n]|$)", RegexOptions.Compiled | RegexOptions.IgnoreCase); 

    private  Regex command = new Regex(@"(^clone$| clone$| clone |^clone )", RegexOptions.Compiled | RegexOptions.IgnoreCase);

     private  Regex username = new Regex(@"--username[\s]+([^\s\""]+|\""[^\""]*\"")", RegexOptions.Compiled | RegexOptions.IgnoreCase);

    private  Regex passwd= new Regex(@"--password[\s]+([^\s\""]+|\""[^\""]*\"")", RegexOptions.Compiled | RegexOptions.IgnoreCase); 

    private  Regex error = new Regex("(?:file '(?<file>.*?)')|(?:Directory '(<file>.*?)')", RegexOptions.IgnoreCase); 

     

    Now these are all-valid strings (or can say regexs ) in C# but if i feed these to my Java program ,it doesnt extract out double quotes well because of  \"" in all strings.

    if  i replace  it with "" everything is fine.

    But i cannt change the input coming from different sources.I want my Java regex to handle this.

    I know there can be many such cases to break the code.(if input can be from multiple sources,languages)but  atleast i know this one to handle.

     

    So please suggest me the addition to my regex to handle such cases.

     

View as RSS news feed in XML