简体   繁体   中英

Python Regex to find particular in sentence

I am very new to regex I am searching in following sentence:

OCEU0H - Debt securities issued by multilateral development company a. OCEU0J - Debt securities issued by private company

Out should be :- ['OCEU0H','OCEU0J']

OC is always at start and EU will be any input country code.

My try is :- r'\\b+(^(OC)\\W+)\\b+'

Also please suggest me the best tutorial or document for regex.

You need to use

\bOC\w+\b

See the regex demo

To match word chars, you need to use \\w , not \\W . Note that if you only want to match ASCII letters after OC , it makes more sense to use [A-Za-z] rather than \\w (any letter, digit or _ ).

You should not quantify zero-width assertions like \\b (word boundaries) and should not use the ^ anchor before OC as ^ matches the start of sting.

Pattern details

  • \\b - a word boundary
  • OC - a literal substring
  • \\w+ - 1 or more word chars (or any 1+ ASCII letter if you use [A-Za-z]+ )
  • \\b - a word boundary

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM