Showing posts with label Regex. Show all posts
Showing posts with label Regex. Show all posts

Thursday, 26 September 2013

Java: Code to unquote string using Regex


str="\"hello\"" // or "'hello'"

public static unquote(String str)
{
        str = str.trim();
if (str.startsWith("'") && str.endsWith("'"))
{
    str = match(str, "(?<=').*(?=')");
}
else if (str.startsWith("\"") && str.endsWith("\""))
{
         str = match(str, "(?<=\").*(?=\")");
}
}

//matches the pattern
private static String match(final String string, final String regex)
{
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
matcher.find();
return matcher.group(); 
}

Sunday, 7 July 2013

Regex to find non-quoted string match

Created a below regex to  identify , that is not quoted. 

(,) (?=(?:[^\"]|\"[^\"]*'\")*$),

(,) 
    (?=   
           (?:                               # non capturing group
                 [^\"]|                     # string not quoted 
                 \"[^\"]*'\"           # string inside quotes
            )*$                            # many a times until end of string
     )


This has a an issue when you really run it with a lengthy string
positive lookahead  + alteration +  * + lengthy string  +  heap size crunch.




So the below regex will fetch you the 

(,)(?=(?:[^"]*"[^"]*")*[^"]*$)

(,)
   (?=                            # look ahead
       (?:                         # non capturing group, ignore string within group 
           [^"]*                 # group of non-quoted text  
           "[^"]*"               # followed by quoted quote + non quoted (*)+ quote  
        )*                        # this group can be 0 or many
    [^"]*                       # capturing group    (non quoted)
   $)                            # till end of string



Regex Tester Online: http://regexpal.com/


String used to Test 
(state "locked" OR state" not active" NOT state"pending or " OR state"running" AND state" and completed" OR state"error" OR state"warning") AND NOT hidden"true" AND NOT type"Background" AND NOT cycle"true" AND parent"null" AND test"asdfasdf asdfhasd fha sdfhas udfhuasdhfu asudfhasudfhuas fhuihas udfhuashd ufhasuidfhua shdfuas hduhuhasd fuhasdufhas duhuhu hasufhuasdhfdfsdfasdfsdasdsad7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777aasda777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777sd7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777as7777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777d7end")