Regex Tools |
grep |
PowerGREP |
RegexBuddy |
RegexMagic |
General Applications |
EditPad Lite |
EditPad Pro |
Google Docs |
Google Sheets |
LibreOffice |
Notepad++ |
Databases |
Google BigQuery |
MySQL |
Oracle |
PostgreSQL |
Python.NET is a package that gives Python programmers nearly seamless integration with .NET Framework. IronPython is an open-source implementation of the Python programming language which is tightly integrated with .NET. Both allow developers to write Python code that can use both Python modules and .NET classes. This means both allow developers to use Python’s re module and .NET’s regex class.
You can use Python’s re module with Python.NET and IronPython in exactly the same way you would use it with the original implementation of Python (CPython). Add import re to your code and then call re.search(), re.sub(), re.split(), and the like as usual. You’ll then be using Python’s regular expression syntax.
But, you may prefer to use the .NET Regex class. After all, the benefit of Python.NET and IronPython over the original Python is that they make it easy for Python code to consume .NET classes. You’ll then be using .NET’s regular expression syntax. It has some features that Python’s regular expression syntax lacks, including balancing groups, unrestricted lookbehind, and conditionals. Since your Python code will be running in a .NET environment you may also find a performance benefit in using the .NET Regex class.
Do keep in mind that there are many differences between the Python and .NET regex flavors. An obvious one is the different syntax for named capturing groups and backreferences. But even when the syntax is the same, there are many small differences in exactly what is matched. They all add up to the point where you can’t simply substitute one regex engine for the other and expect all your regexes to work in exactly the same way.
To use the .NET Regex class you first need to import .NET’s System.Text.RegularExpressions namespace. How to do that differs between Python.NET and IronPython.
With Python.NET you can load the classic .NET Framework (.NET 1.0 to 4.8.x) as follows:
from pythonnet import load load("netfx")
With Python.NET you can load .NET Core (any version) or load .NET 5.0 or later and then reference the namespace as follows:
from pythonnet import load load("coreclr") import clr clr.AddReference("System.Text.RegularExpressions")
With IronPython, you only need to reference the namespace:
import clr clr.AddReference("System.Text.RegularExpressions")
Once that's done, everything works the same in Python.NET and IronPython. You can now import the .NET Regex class and related types so you can use them without having to spell out System.Text.RegularExpressions each time:
from System import ArgumentException from System.Text.RegularExpressions import *
With the namespace imported, you can use the Regex class as explained in the topic about .NET. This sample Python code uses the Regex class to check whether a regex can find a match in a string:
try: regexobj = Regex(r"\A\w+\z") if regexobj.IsMatch(subject): print("Successful match") else: print("Match attempt failed") except ArgumentException: print("Syntax error in the regular expression")
| Quick Start | Tutorial | Search & Replace | Tools & Languages | Examples | Reference |
| grep | PowerGREP | RegexBuddy | RegexMagic |
| EditPad Lite | EditPad Pro | Google Docs | Google Sheets | LibreOffice | Notepad++ |
| Boost | C# | Delphi | F# | GNU (Linux) | Groovy | ICU (Unicode) | Java | JavaScript | .NET | PCRE (C/C++) | PCRE2 (C/C++) | Perl | PHP | POSIX | PowerShell | Python | Python.NET and IronPython | R | RE2 | Ruby | std::regex | Tcl | TypeScript | VBScript | Visual Basic 6 | Visual Basic (.NET) | wxWidgets | XML Schema | XQuery & XPath | Xojo | XRegExp |
| Google BigQuery | MySQL | Oracle | PostgreSQL |
Page URL: https://www.regular-expressions.info/pythonnet.html
Page last updated: 11 October 2025
Site last updated: 14 October 2025
Copyright © 2003-2025 Jan Goyvaerts. All rights reserved.