简体   繁体   中英

How to write regex to extract single words from parentheses?

I have a long string that contains text. I want to extract every word in between parentheses (' ') , but only as long as there is no more than one word in between the parentheses (IE, no spaces).

For example, in the below:

<ul>
<li>7-SPEED DOUBLE CLUTCH AUTOMATIC TRANSMISSION (2TC)</li>
<li>MOONLIGHT BLACK SOFT TOP (3YA)</li>
<li>8-WAY PWR FRONT BUCKET SEATS -inc: driver memory, 2-way adjustable headrests, exterior mirror memory (459)</li>
<li>BLACK SAPPHIRE METALLIC (475)</li>
<li>HEATED FRONT SEATS (494)</li>
<li>GLACIER SILVER ALUMINUM TRIM (REQ: ZMP M Sport Pkg) (4MG)</li>
<li>NAVIGATION SYSTEM -inc: hard drive, HD Radio, real-time traffic info, voice command, cupholders (609)</li>
<li> ASSIST W/BLUETOOTH -inc: (4) year subscription (w/609 Navigation System-inc: Online info services) (639)</li>
<li>IPOD & USB ADAPTER (6FL)</li>
<li>M SPORT STEERING WHEEL-MOUNTED SHIFT PADDLES (REQ: ZSP Sport Pkg or ZMP M Sport Pkg & 205 Auto Trans) (7XA)</li>
<li>BLACK, BOSTON LEATHER SEAT TRIM (LWSW)</li>
<li>M SPORT PKG -inc: 18&quot; x 7.5&quot; front & 18&quot; x 8.5&quot; rear double-spoke light alloy wheels (style 261M), P215/40R18 front & 245/35R18 rear performance tires, 8-way manual front sport seats, M sport steering wheel, shadowline exterior trim, increased top speed limiter (w/205 Auto Trans REQ: 7XA Shift Paddles) (ZMP)</li>
<li>PREMIUM PKG -inc: Boston leather seat trim, universal garage door opener, auto-dimming pwr folding exterior mirrors w/memory, auto-dimming rearview mirror w/digital compass, pwr front seats w/pwr lumbar support,  Assist w/(4) year subscription, Bluetooth, illuminated exterior door handles, front/rear reading lights, front footwell illumination, driver/front passenger illuminated vanity mirrors, ambiance interior lighting (ZPP)</li>
<li>VALUE PKG -inc: Boston leather seat trim, iPod and USB adapter (ZVP)</li>
</ul>

I want to get as a result

2TC 3YA 459 475 494 4MG 609 639 6FL 7XA LWSW ZMP ZPP ZVP

What is the best way to parse this? I'm implementing this in C# if it matters

The regex \\((\\S+)\\) should do the trick.

string pattern = @"\((\S+)\)";
MatchCollection matches = Regex.Matches(str, pattern);

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