How to Rewrite rule by using regular expression (Regex) in Charles Proxy to modify quest param

How to use Regular expression (Regex) to rewrite rules in Charles Proxy

In this article, I are going to show you how to use Regular expression (Regex) to rewrite rules in Charles Proxy to modify quest param or modify query param for testing. Regular expression is the best way to catch the data that always keeps change in requests or responses. Please check more details.


There are a lot of reasons we need to change a certain number, parameter or data in request and response. The simple way is to interrupt request or response before request sent out or before client receives response. However, sometimes when request or response wraps too many data, it will take your time to find right place where you want to make change. And generally, when client end waits for too long, it will be timeout. After timeout, even the modified response received by client, client end will ignore the response returned. In order to change data right away while request sent out or while response received by client, the best way is to use Regular Expression (Regex) to let Charles change data right away without time delay.

I have encountered an issue that a value of parameter keeps change all the time that we cannot catch the values to make them change. For example, we would like to change the value of latitude and longitude to another location for geo-restriction testing. However, every time the request sent out from phone device,
the latitude and longitude of the last 2 digital numbers keeps change from time to time even not move phone anywhere. It becomes impossible to match latitude and longitude with whole number without using Regular expression (Regex). Let's give an example and show you how to solve this problem.

Here is example for the request that sends latitude and longitude:
 http://www.hostname.com/ajax?action=6&version=3432&location=CA-ON-TO&latitude=43.564633&longitude=-79.344321


In the Rules:
Type: Modify Query Param

Match:
    Name: latitude
    Value: 43\.\d*        -- Make sure to have "Regex" option selected
Replace:   
    Name: latitude
    Value: 40.12345

When select Modify Query Param for the Type, make sure to input value in Match Name field. Otherwise it will throw out exception in Error Log. For more details how to debug it, you can check the following video.



Regular special characters in regular expressions.

\   Interpreted literally depends on special character or not. "a*" matches "a" or "aaaa", and "a\*" matches "a*" in "a*bc" because "*" is special character. "b" matches any lowercase  "b" when "b" is not a special character if don't put "\" in front.  '\b" matches any word not followed or proceeded with another word. For example, "le\b" or '\bc" both match "Apple"

*  Matches the preceding expression 0 or more times.
     "g*" or "g{0,}" matches "g" and "google"

+  Matches the preceding expression 1 or more times.
    "p+" or "p{1,}" matches  "pp" in "Apple" 

?  Matches the preceding expression 0 or 1 time.
    "p?" or "p{0,1}" matches the first "p" in "Apple"

^  Start with character followed. 
     "^A" matches "Apple"

$  End with character followed. 
     "$e" matches "Apple"

\d  Matches any digital number. 
     "\d" or [0-9] matches "3" in words of "This is number A3"

\D Matches any non-digital number. 
     "\D" or [^0-9] matches "A" in words of "This is number A3"

\w Matches any alphanumeric character.  
  "\w" or [A-Za-z0-9] matches 'a' in "apple," '5' in "$5.28," and '3' in "3D."

\W  Matches any non-word character.
    "\W" or [^A-Za-z0-9] matches '&' in "A&b"