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

What are the common rules for regular expressions

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

Share

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

This article will explain in detail what are the common rules of regular expressions. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Let's take a look at the common rules of regularity:

One picture speaks for itself, ha!

Methods:

Find () lookup

Findall () finds everything

Replace () replace

Split () segmentation

Search () global matching

Match () matches from the starting position

Sub () is also a replacement, which is simpler than replace

Complie () declares a regular for later use

Modifier:

Re.I makes matches case sensitive

Re.M multiline matching

Re.S makes. Match all characters, including line breaks (most commonly used)

Re.U parses characters according to the Unicode character set

Example:

Import re

S='hello world'

Print (s.find ('ll')) # find ll

Ret=s.replace ('ll','xx') # replace ll

Print (ret)

Print (s.split ('')) # has more space division

Ret=re.findall ('w\ w {2} Lindsay hello world')

Print (ret) # matches worl and returns a list

Ret=re.findall ('w. LBG. Hello w\ tld') #. (wildcard) means to match a character, except for\ n

Print (ret) # matches w\ tl

Ret=re.findall ('h.odyne dsadashello`)

Print (ret) # matches hello

The # metacharacter $matches at the end

Ret=re.findall ('h. Odyne, girls, dsadashello.')

Print (ret) # matches hello and must end with o

# *: repeat match from 0 to infinity

# ret=re.findall ('h.roomodyne dsdfgfhellojbcvbtr')

# print (ret) # match out hello greedy match, match as many matches as possible, you can add an o at the end to see if it will be another result)

# +: repeat matching 1 to infinity

Ret=re.findall ('h.furology, dsdfgfhojbcvbtr')

Print (ret) # did not match the content

#? : [0,1]

Ret=re.findall ('h.furology dsdfgfhooojbcvbtr')

Print (ret) # matching divided by hoo

# {}: define it yourself

Ret=re.findall ('a {5, 5, 10} bachelors, girls, etc.)

Print (ret)

# []: character set [amurz], which can cancel the special function of metacharacters\ ^-except

Ret=re.findall ('a [a _ r _ b _ r c] x _ e _ e _ r _ c _ x')

Print (ret) # [aheline bjorc] matches only one character in the middle

Ret=re.findall ('[., *, $,]', 'acx.sd*fg$tyt,')

Print (ret)

# [^ t] except for t

Ret=re.findall ('[^ ts,5]', 'ts456t,tt')

Print (ret)

Findall (): all the results are returned in a list

Search (): returns an object (object), which needs to call group ()

Match (): matches only at the beginning of the character, and returns an object, which needs to call group ()

Split (): split

=

Ret=re.split ('[s recorder kjime d]', 'dsajk')

Print (ret) # Segmentation from smeno kjol d

=

Ret=re.sub ('a.. Xanthology, thecontrol, thecontrol, and so on)

Print (ret) # replaces the matching string with s, which is also a replacement

=

Ret=re.compile ('\ .com') declares a regular so that it can be called later

Obj=ret.findall ('dsasds.comgfdhgd')

Print (obj) # matches .com

This is the end of this article on "what are the common rules of regular expressions?". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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