Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse Python regular expressions

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to parse Python regular expressions, in view of this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

Today I'll show you how regular expressions are used in Python. The reason for this is that regular expressions are not unique to Python, but are self-contained and are used in many places. In Python, regular expression is mainly realized by re module, so learning Python regular expression is mainly about learning re module, and then you need to be familiar with the language of regular expression, so you can basically master it.

# re module

The functions commonly used in the re module are

Compile, findall,match,search,sub,split

The compile function compiles a regular expression template and returns an object (instance) of the template. The complie function is generally used with findall, findall, which means that after you have a template object, use this object to match the string you want to match, and then return all the matches as a list. The function search is also used to find the character matched by the template object in a string, and only the first one is returned if the match is successful multiple times. The match function only matches the beginning of the string and returns None if the match fails. The sub function means to replace, split is to divide the string according to the specified characters, and the main difference of Python string segmentation is that you can select multiple separators, while the segmentation method that comes with Python strings can only select one separator. Let's write a simple chestnut, no matter why it matches that, it's just a chestnut to show you what the syntax of regular expressions looks like.

This article is relatively simple, the reason for this is because we do not want to make people think that there is a lot of regular expressions, in fact, it is not as difficult as you think. It is enough to learn these things. The article is not typeset, and the main author is too lazy. If you don't like it, you can take a look at a reference link below.

#-*-coding:utf-8-*-

Import re

# complie, findall, search, match, sub, split

Test_string = 'abc | 2abcd) 3Hello world4abcddd,\ n 2017-11-19 | Python regular expression details'

Patterns = [

"Hello world", # matches Hello world

"\ d +", # matches all digits,\ d indicates matching numbers, and the + sign indicates matching the first character once or infinitely.

"abcd?", # matches abc and abcd,? The number indicates that the previous character is matched 0 or 1 times.

"abcd*", # asterisk * matches the previous character 0 or countless times

"\ w", # lowercase w matches word characters

"\ W", # matches non-word characters

"\ d+\ -\ d+\ -\ d+", # matches dates separated by horizontal lines

"^ a [bc] d?", # ^ indicates the beginning of the matching string

"\ |.", #\ indicates the escape character. Indicates that matches characters other than newline characters

"\ d", # matching numbers

"\ d+ |\ w+", # | matches any one of the left and right expressions

"\ s", # matches white space characters

"\ s\ w+", # matches the white space character followed by the word character

".", # matches characters other than newline characters

"\ D", # matches non-numeric characters

"\ s", # uppercase S matches any non-empty character

]

For pattern in patterns:

Print "-" * 20

Print ("current regex is: {}" .format (pattern))

P = re.compile (pattern) .findall (test_string)

If p:

Print ("findall results is: {}" .format (p))

Else:

Print ("[!] match failed")

P1 = re.search (pattern, test_string, flags=0)

If p1:

Result = p1.group ()

Print ("search results is: {}" .format (result))

Else:

Print ("[!] match failed")

P2 = re.match (pattern, test_string, flags=0)

If p2:

Result = p1.group ()

Print ("match results is: {}" .format (result))

Else:

Print ("[!] match failed")

Print ("-" * 20)

Test_string = '2017. 11. 19'.

Re_sub = re.sub (",", ":", test_string)

Print (re_sub)

Print ("-" * 20)

Test_string = "2017. 11. 19."

Re_split = re.split (",", test_string)

Print (re_split)

"

This is the end of the regular expression, after the introduction, we should mainly practice more, and then try more combined with specific tasks. If you can master it well, it will save a lot of trouble.

"

--

Current regex is: Hello world

Findall results is: ['Hello world']

Search results is: Hello world

[!] Match failed

--

Current regex is:\ d +

Findall results is: ['2,'3,'4, '2017,' 11,'19]

Search results is: 2

[!] Match failed

--

Current regex is: abcd?

Findall results is: ['abc',' abcd', 'abcd']

Search results is: abc

Match results is: abc

--

Current regex is: abcd*

Findall results is: ['abc',' abcd', 'abcddd']

Search results is: abc

Match results is: abc

--

Current regex is:\ w

Findall results is: ['axed,' baked, 'cased,' 2','a', 'baked,' cased, 'dashed,' 3, 'hacked,' eyed, 'lumped,' lumped, 'obliterated,' oiled, 'rusted,' lumped, 'dusted,' 4 percent, 'axed,' baked, 'clocked,' dumped, 'dusted,' 'dice,' 2,'0,'1','7','1','1','1','9','p','y', 'taper,' hinge, 'oasis,' nasty, 'positive', 'then', 'table','da', 'form', 'detailed', 'solution']

Search results is: a

Match results is: a

--

Current regex is:\ W

Findall results is: ['|',')', -','-,'|']

Search results is: |

[!] Match failed

--

Current regex is:\ d+\ -\ d+

Findall results is: ['2017-11-19']

Search results is: 2017-11-19

[!] Match failed

--

Current regex is: ^ a [bc] d?

Findall results is: ['ab']

Search results is: ab

Match results is: ab

--

Current regex is:\ |.

Findall results is: ['| 2percent,'| P']

Search results is: | 2

[!] Match failed

--

Current regex is:\ d

Findall results is: ['2,'3,'4,'2,'0,'1,'7,'1,'1,'1,'9']

Search results is: 2

[!] Match failed

--

Current regex is:\ d + |\ w +

Findall results is: ['abc',' 2, 'abcd',' 3, 'Hello',' world4abcddd', '2017,' 11,'19, 'Python regular expression details']

Search results is: abc

Match results is: abc

--

Current regex is:\ s

Findall results is: ['','\ nfolk,']

Search results is:

[!] Match failed

--

Current regex is:\ s\ w +

Findall results is: ['world4abcddd',' 2017']

Search results is: world4abcddd

[!] Match failed

--

Current regex is:.

Findall results is: ['axed,' baked, 'crested,' |','2', 'axed,' baked, 'crested,' dashed,')','3', 'hash,' eBay, 'lump,' lump, 'oily,' oily, 'lager,' dashed,'4 percent, 'axed,' b' 'canals,' dudes,'2','0','1','7','-','1','1','1','9,'|, 'playing,' yearly, 'tween,' hinge, 'oval,' nasty, 'positive,' then, 'table' 'da', 'form', 'detailed', 'solution']

Search results is: a

Match results is: a

--

Current regex is:\ d

Findall results is: ['axed,' baked, 'crested,' |', 'axed,' baked, 'cased,' dashed,')', 'hacked,' eyed, 'lumped,', 'oiled,' rusted, 'lumped,' dashed, 'axed,' baked, 'clocked,' dumped, 'dusted,' 'During, 'nasty,','-','-','|', 'Please,' yearly, 'tweak,' hitch, 'oasis,' nasty, 'positive,' then', 'table','da', 'detailed', 'solution']

Search results is: a

Match results is: a

--

Current regex is:\ s

Findall results is: ['axed,' baked, 'crested,' |','2', 'axed,' baked, 'crested,' dumped,'3', 'hailed,' eyed, 'lumped,' lumped, 'walled,' oiled, 'rusted,' lumped, 'dashed,' 4 percent, 'axed,' baked,'c' 'dashed,' dumped, 'dashed,', '2percent,' 2percent, '0percent,' 1percent, '7percent,' -', '1percent,' 1percent,'-', '1percent, 9percent,' |, 'packing,' yawning, 'tying,' haggling, oval, 'nasty,' positive, 'then', 'table', 'reaching', 'formula' 'detailed', 'solution']

Search results is: a

Match results is: a

--

2017:11:19

--

['2017','11','19']

This is the answer to the question about how to parse the Python regular expression. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report