最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Regex - non capturing group - Stack Overflow

programmeradmin0浏览0评论

I currently have the following regex:

Regex101:

iPhone\s*(SE(?:.*?)\d)|(\d+)|(X)|(SE)

for the test string

iPhone 6S Plus
iPhone SE
iPhone 7
iPhone 8 Plus
iPhone X
iPhone XS
iPhone 11 Pro Max
iPhone 12 Mini
iPhone 13 Pro Max
iPhone SE
iPhone SE2
iPhone SE3
iPhone SE (1st Generation)
iPhone SE (2nd Generation)
iPhone SE (3rd Generation)

My goal is to extract all the identifiers.

iPhone 6S Plus              => 6
iPhone SE                   => SE
iPhone 7                    => 7
iPhone 8 Plus               => 8
iPhone X                    => X
iPhone XS                   => X
iPhone 11 Pro Max           => 11
iPhone 12 Mini              => 12
iPhone 13 Pro Max           => 13
iPhone SE                   => SE
iPhone SE2                  => SE2
iPhone SE3                  => SE3
iPhone SE (1st Generation)  => SE1
iPhone SE (2nd Generation)  => SE2
iPhone SE (3rd Generation)  => SE3

The thing where my regex lacks is the SE1/2/3 recognition as i have to ignore the ' (' while capturing. It would be great if the identifier result would always be captured in group 1.

I currently have the following regex:

Regex101: https://regex101/r/OIkya1/2

iPhone\s*(SE(?:.*?)\d)|(\d+)|(X)|(SE)

for the test string

iPhone 6S Plus
iPhone SE
iPhone 7
iPhone 8 Plus
iPhone X
iPhone XS
iPhone 11 Pro Max
iPhone 12 Mini
iPhone 13 Pro Max
iPhone SE
iPhone SE2
iPhone SE3
iPhone SE (1st Generation)
iPhone SE (2nd Generation)
iPhone SE (3rd Generation)

My goal is to extract all the identifiers.

iPhone 6S Plus              => 6
iPhone SE                   => SE
iPhone 7                    => 7
iPhone 8 Plus               => 8
iPhone X                    => X
iPhone XS                   => X
iPhone 11 Pro Max           => 11
iPhone 12 Mini              => 12
iPhone 13 Pro Max           => 13
iPhone SE                   => SE
iPhone SE2                  => SE2
iPhone SE3                  => SE3
iPhone SE (1st Generation)  => SE1
iPhone SE (2nd Generation)  => SE2
iPhone SE (3rd Generation)  => SE3

The thing where my regex lacks is the SE1/2/3 recognition as i have to ignore the ' (' while capturing. It would be great if the identifier result would always be captured in group 1.

Share Improve this question asked Mar 14 at 12:23 CrazyEightCrazyEight 4878 silver badges27 bronze badges 7
  • 1 What regex version and what programming language are you using? There are many variations between the different implementations. – AdrianHHH Commented Mar 14 at 12:27
  • 1 i would like to just capture the first digit that comes up after the SE... so its not a MUST that there is a ' (' But ty I think the non capture around it did the magic – CrazyEight Commented Mar 14 at 12:32
  • 1 If you are using C#, you could also use the same named group for the numbers: iPhone\s*(?:(SE)(?:.*?(?<number>\d+))?|(?<number>\d+)|(X)) see regex101/r/xCT0F5/1 The thing is that if you want to get SE1 it will be a concatenation of the group 1 and the named group. – The fourth bird Commented Mar 14 at 12:51
  • 1 Well I use c# :) Works like a charm TY dotnetfiddle/kN9tU0 – CrazyEight Commented Mar 14 at 13:02
  • 2 It is ideone/znQqUj – Wiktor Stribiżew Commented Mar 14 at 13:54
 |  Show 2 more comments

1 Answer 1

Reset to default 4

Try matching:

iPhone\s*(X|\S+)(?:(?<=SE)\s*\((\d+))?

and list results with:

$1$2

See: regex101


Explanation

MATCH:

  • ^iPhone\s*: Anchor to start of string and literal "iPhone" succeeded by any amount of spaces.
  • ( ... ): Capture to group 1
    • X: either an "X"; this will also match for "XS" and avoid matching the "S", or
    • \S+: any numer of non-space characters.
  • (?: ... )?: Optionally,
    • (?<=SE): if the previously matched has been "SE"
    • \s*\(: match spaced and a literal "(" before
    • (\d+): capturing any following digits to group 2

The result will now be a concatination of groups 1 and 2.

发布评论

评论列表(0)

  1. 暂无评论