简体   繁体   中英

first name and title without parenthesis in a single regex

how to put the following 2 regex together to capture both groups in one expression:

(^[a-zA-Z]*) should capture the first name, first_here

\\((.+?)\\) should capture the title

to extract:

Donald President
Mike Vice President

from:

Donald Trump (President)
Mike Pence (Vice President)

You can combine as follows to match first name and title. then use first and second captured group to form output.

^([a-zA-Z]*).*?\(([^)]+?)\)

Demo

You might use

^(?P<name>\w+)[^()]+\((?P<function>[^()]+)\)

See a demo on regex101.com .


Obviously this will work for

Cake Cookie (Sugar)

as well. If you want to limit it to names only, use a database.

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