<commit_before>---
layout: post
title: Intro to Regular Expressions in Python
---

## A Quick Introduction to Regular Expressions in Python

Regular expressions (regex) are a concise way of matching patterns in strings. You can use regular expressions in Python by importing the _re_ [module](https://docs.python.org/3/library/re.html#module-re) like so: `import re`.

You can match characters with themselves (ie. A-Z, 1-9, etc.) or you can use metacharacters which are used to match unusual patterns. These are the metacharacters you can use in regex: `. ^ $ * + ? { } [ ] \ | ( )`

### Character Classes
`[` and `]` are used for specifying a set of characters you would like to match (a character class). For example, `[abc]` or `[a-c]` will match any of the lowercase letters _a_, _b_ or _c_. Similarly, `[123]` or `[1-3]` will match any of the characters _1_, _2_ or _3_. Keep in mind that metacharacters won't work within the brackets used to define a character class. You can negate a character class using the carat `^` like this: `[^5]`, which matches all characters except _5_. 

### Backslash
The most useful metacharacter is the backslash `\`, which can be used to escape other metacharacters (eg. `\[` will escape the [) or indicate special sequences (eg. \w matches any alphanumeric character). Also, keep in mind that `\` escapes itself (eg. `\\`).

The [Python docs](https://docs.python.org/3/howto/regex.html) list some of the more useful backslash sequences:

* `\d` matches any decimal digit, eg. `[0-9]`
* `\D` matches any non-digit character, eg. `[^0-9]`
* `\s` matches any whitespace character, eg. `[ \t\n\r\f\v]`
* `\S` matches any non-whitespace character, eg. `[^ \t\n\r\f\v]`
* `\w` matches any alphanumeric character, eg. `[a-zA-Z0-9_]`
* `\W` matches any non-alphanumeric character, eg. `[^a-zA-Z0-9_]`

### Useful Links
<commit_msg>Update 2017-09-30-Regex.md<commit_after>---
layout: post
title: Intro to Regular Expressions in Python
---

## A Quick Introduction to Regular Expressions in Python

Regular expressions (regex) are a concise way of matching patterns in strings. You can use regular expressions in Python by importing the _re_ [module](https://docs.python.org/3/library/re.html#module-re) like so: `import re`.

You can match characters with themselves (ie. A-Z, 1-9, etc.) or you can use metacharacters which are used to match unusual patterns. These are the metacharacters you can use in regex: `. ^ $ * + ? { } [ ] \ | ( )`

### Character Classes
`[` and `]` are used for specifying a set of characters you would like to match (a character class). For example, `[abc]` or `[a-c]` will match any of the lowercase letters _a_, _b_ or _c_. Similarly, `[123]` or `[1-3]` will match any of the characters _1_, _2_ or _3_. Keep in mind that some of the metacharacters won't work within the brackets used to define a character class. You can negate a character class using the carat `^` like this: `[^5]`, which matches all characters except _5_. 

### Backslash
The most useful metacharacter is the backslash `\`, which can be used to escape other metacharacters (eg. `\[` will escape the [) or match special sequences (eg. \w matches any alphanumeric character). Keep in mind that `\` can be used to escape itself (eg. `\\`).

The [Python docs](https://docs.python.org/3/howto/regex.html) list some of the more useful backslash sequences:

* `\d` matches any decimal digit, eg. `[0-9]`
* `\D` matches any non-digit character, eg. `[^0-9]`
* `\s` matches any whitespace character, eg. `[ \t\n\r\f\v]`
* `\S` matches any non-whitespace character, eg. `[^ \t\n\r\f\v]`
* `\w` matches any alphanumeric character, eg. `[a-zA-Z0-9_]`
* `\W` matches any non-alphanumeric character, eg. `[^a-zA-Z0-9_]`

### Useful Links

{'fraction_non_alphanumeric': 0.13019169329073482, 'fraction_numerical': 0.013844515441959531, 'mean_word_length': 3.866580310880829, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '744786', 'n_tokens_mistral': 1242, 'n_tokens_neox': 1196, 'n_words': 513}