> For the complete documentation index, see [llms.txt](https://www.jinshupeethambaran.com/articles/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.jinshupeethambaran.com/articles/engineering/regex-for-sensitive-data.md).

# RegEx for Sensitive Data

Here is the regex we can use it in the scripts (python I preferred).

```regex
    'password': r'(?i)(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()\-_=+{};:,<.>]).{8,}',
    'jwt_token': r'(?i)Bearer\s+(?:[A-Za-z0-9\-_~+\/]+=*\.)+[A-Za-z0-9\-_~+\/]+=*',
    'credit_card': r'\b(?:\d[ -]*?){13,16}\b'
```

In this code, the pattern <mark style="color:blue;">`r'(?i)(?=.*[a-zA-Z])(?=.*\d)(?=.*[!@#$%^&*()\-_=+{};:,<.>]).{8,}'`</mark> is used to identify potential passwords. Let's break down the pattern:

* <mark style="color:blue;">`(?i)`</mark> makes the pattern case-insensitive.
* <mark style="color:blue;">`(?=.*[a-zA-Z])`</mark> ensures that the potential password contains at least one letter.
* <mark style="color:blue;">`(?=.*\d)`</mark> ensures that the potential password contains at least one digit.
* <mark style="color:blue;">`(?=.*[!@#$%^&*()\-_=+{};:,<.>])`</mark> ensures that the potential password contains at least one special character.
* <mark style="color:blue;">`.{8,}`</mark> enforces a minimum length requirement of 8 characters for the potential password.

<mark style="color:blue;">`'jwt_token': r'(?i)Bearer\s+(?:[A-Za-z0-9\-_~+\/]+=*\.)+[A-Za-z0-9\-_~+\/]+=*'`</mark>: This pattern is designed to match potential JWT tokens preceded by the word "Bearer". It looks for sequences of alphanumeric characters, dashes, underscores, tildes, plus signs, forward slashes, and periods that resemble JWT tokens.

<mark style="color:blue;">`'credit_card': r'\b(?:\d[ -]*?){13,16}\b'`</mark>: This pattern is used to identify potential credit card numbers. It matches sequences of digits that have a length of 13 to 16 characters, allowing for optional spaces or dashes between the digits.

<mark style="color:red;">These patterns are just examples and may require further refinement based on the specific formats and validation rules of JWT tokens and credit card numbers in your use case. Make sure to adapt the patterns according to your requirements.</mark>

<mark style="color:red;">Remember to implement additional security measures, such as secure token handling, proper credit card validation, and compliance with relevant data protection regulations, to ensure the sensitive data is handled securely</mark><mark style="color:blue;">.</mark>
