Monday, July 17, 2017

Java Regular Expression

Regex defines the the search pattern of strings in Java.

Regex consists of set of symbols. Following are the common matching symbols used for regex purpose.



Symbol
Description
.
Any character
^      $
Regex matches beginning of the line and end of the line
[abc][pq]
Matches a/b/c followed by p/q
[^abc]
Negates the matching pattern
[p-z4-9]
Matches characters between p and z or 4 to 9.
p|q
Equivalent to [pq]
pq
q immediately follows p


These matching symbols are further defined by quantifiers which will be defining number of occurrences of regex pattern.


Regex
Description
{p,q}
Occurs between p to q times
{p}
Occurs p times
*
{0,}
+
{1,}
?
{0,1}


There are certain meta characters that can be used to simplify defining symbols.


Symbol
Description
\s
Equivalent of  [ \t\n\x0b\r\f]
\S
[ ^\t\n\x0b\r\f]
\w
[A-Za-Z0-9_]
\W
[^\w]
\d
[0-9]
\D
[^0-9]
\b
\w

Note: Grouping of regex elements can be done using ( ) operators.
          p(?!q) denotes regex matches iff p is not followed by q.

Eventually we can consider following example, where I wanted to identify following string

{
               "@nil":"true"
            }


and replace this string with empty("") String.

Input String;

{
   "Opportunities":{
      "Opportunity":[
         {
            "Name":"Firzhan",
            "Age":"30 years",
            "EPF_Start_Date__c":{
               "@nil":"true"
            },
            "EPF-Id":"006E000000",
            "EPF_End_Date__c":{
               "@nil":"true"
            },
            "EPF_Closed_Date__c":{
               "@nil":"true"
            },
             
"CreatedDate":"2016-12-12+05:30",
            "Location":"Dehiwala"
         },
         {
            "Name":"Firzhan2",
            "Age":"31 years",
             "Puppies":{
               "@nil":"true"
            },

            "EPF-Id":"006E000000",
        
            "EPF_Start_Date__c":"2017-03-25+05:30",
            "EPF_End_Date__c":"2017-03-02T00:00:00.000+05:30",
            "EPF_Closed_Date__c":"2017-07-17T12:26:46.000+05:30",
            "CreatedDate":"2013-12-12+05:30",
             "Location":"Dehiwala"
         }
      ]
   }
}


Pattern pattern = Pattern.compile(Utils.JSON_REGEX_CONSTANT);
//Matcher matcher = pattern.matcher(opportunityJson.replaceAll("^\\s|\n\\s|\\s$", "''"));Matcher matcher = pattern.matcher(jsonResponse);
jsonResponse = matcher.replaceAll("\"\"");

Here JSON_REGEX_CONSTANT is equal to 

([\{]{1}[\s|\n]*(\"@nil\"){1}(\s*|\n*[:]+\s*|\n*)(\"true\"){1}[\s|\n]*[\}]{1})

No comments:

Post a Comment