Regular expression symbolic group name in Python / Ruby

Regular expressions in python have a feature called symbolic group names. If you use this, you can name and manage the matched substrings, so it will be more readable than using \ 1. The following is an example.

# encoding: utf-8     
import re          
testtext = 'abcdefghijklmnopqrstuvwxyz'

pattern = re.compile(r'.*(?P<test1>k.*t).*')
m = pattern.match(testtext)
if m:
    print(m.group('test1')) 

After all, when the form (? P <name> regular expression) appears in a regular expression, The part that matches the specified regular expression can be retrieved later with the name specified by the group method.

Also, the equivalent function in ruby

/.*(?<test1>k.*t).*/ =~ 'abcdefghijklmnopqrstuvwxyz'
p test1

is. In the case of ruby, it is automatically expanded to a variable. I think I like it.

Recommended Posts

Regular expression symbolic group name in Python / Ruby
Regular expression in Python
Regular expression in Python
Regular expression in regex.h
Use let expression in Python
Python 處 處 regular expression Notes
Use regular expressions in Python
Reproduce the Python regular expression r'\ w (? U)' in JavaScript
AtCoder ABC168 A case expression solved in Ruby and Python
String replacement with Python regular expression
Get the host name in Python
When using regular expressions in Python
Overlapping regular expressions in Python and Java
Differences between Ruby and Python in scope
Big difference in ruby, python, httpd performance
Referencing INI files in Python or Ruby
(Python) HTML reading and regular expression notes
Replace non-ASCII with regular expressions in Python
Don't use \ d in Python 3 regular expressions!
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Features of regular expression modules that I often use personally in Python
How to use regular expressions in Python
Try something like Python for-else in Ruby
How to write Ruby to_s in Python
Pharmaceutical company researchers summarized regular expressions in Python
Match the distribution of each group in Python
GNU GLOBAL (gtags) + α in Go, Ruby, Python
How to write regular expression patterns in Linux
Grouping combination in Python / Ruby / PHP / Golang (Go)
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Meta-analysis in Python
Unittest in python
Regular expression Greedy
Epoch in Python
Discord in Python
Sudoku in Python
DCI in Python
quicksort in python
N-Gram in Python
Programming in python
Plink in Python
[Python] Regular Expressions Regular Expressions
Constant in python
Lifegame in Python.
FizzBuzz in Python
Rock-paper-scissors in Ruby
Sqlite in python
StepAIC in Python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
nCr in Python.
format in python
Scons in Python3