Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

regex - How to back reference "inner" selections ( () ) in a regular expression?

How do you back reference inner parenthesis in Regex?

The sample data is a product price list showing different price breaks based on quantity purchased. The format is quantityLow - quantityHigh : pricePer ; multiples.

I used LINQPad to construct this C# Regex expression to separate the parts, which shows a handy visualization of the Regex data separation. In this example, there are "inner" parenthesis (selections), creating a hierarchical data structure.

string mys = "1-4:2;5-9:1.89";
Regex.Matches (mys, @"((d+)[-|+](d*):(d+.?d*);?)").Dump();  // Graphically show

This breaks down to (Match is everything. Within match, there is a single match and a group match. Within the group match is a few single matches.)

  • MatchCollection (2 items)
    • Group Collection (4 items)
      • CaptureCollection (1 item) () Group "1-4:2;"
      • CaptureCollection (1 item) () Group "1"
      • CaptureCollection (1 item) () Group "4"
      • CaptureCollection (1 item) () Group "2"
    • CaptureCollection (1 item) () Match "1-4;2;"
    • Group Collection (4 items)
      • CaptureCollection (1 item) () Group "5-9:1.89"
      • CaptureCollection (1 item) () Group "5"
      • CaptureCollection (1 item) () Group "9"
      • CaptureCollection (1 item) () Group "1.89"
    • CaptureCollection (1 item) () Match "5-9:1.89"

Just for reference:

  • () parenthesis group found results which can be referenced by a 1..9 (I think).
  • d matches a single digit. The + after matches one or more digits. * after matches zero or more digits. ? after says this match is optional.
  • . matches a single character. . matches a period or decimal in this case.
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just use 1 ... 9 (or $1 ... $9 in some regex implementations) like you normally would. The numbering is from left to right, based on the position of the open paren (so a nested group has a higher number than the group(s) it's nested within).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...