简体   繁体   中英

RegEx to replace entire string with first two values

I'm trying to come up with a regex expression to replace an entire string with just the first two values. Examples:

  • Entire String: AO SMITH 100108283 4500W/240V SCREW-IN ELEMENT, 11"

  • First Two Values: AO SMITH

  • Entire String: BRA14X18HEBU / P11-042 / 310-470NL BRASS 1/4 x 1/8 HEX BUSHING

  • First Two Values: BRA14X18HEBU / P11-042

  • Entire String: TWO-HOLE PIPE STRAP 4" 008004EG 72E 4

  • First Two Values: TWO-HOLE PIPE

The caveat is I'm wanting to preserve any kind of special characters and not count them, like "/"'s and "-"'s. The current code I've written does not, instead leaves the new values entirely blank. Only the first example above works.

Here's what I've got so far:

Matching Value:

^(\w+) +(\w+).+$

New Value:

$1 $2

One option could be using a single capture group and use that in the replacement.

^(\w+(?:-\w+)?(?: +\/)? +\w+(?:-\w+)?).+

The pattern matches:

  • ^ Start of string
  • ( Capture group 1
    • \w+(?:-\w+)? Match 1+ word charss with an optional part to match a - and 1+ word chars
    • (?: +\/)? Optionally match /
    • +\w+(?:-\w+)? Match 1+ word charss with an optional part to match a - and 1+ word chars
  • ) Close group 1
  • .+ Match 1+ times any char (the rest of the line)

If there can be more than 1 hyphen, you can use * instead of ?

Regex demo

Output

AO SMITH
BRA14X18HEBU / P11-042
TWO-HOLE PIPE

A broader match could be matching non word chars in between the words

^(\w+(?:-\w+)*[\W\r\n]+\w+(?:-\w+)*).+

Regex demo

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