Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

regex - How can i capture all data from a certain column?

I want to match all of the data from a specific column, such as pulling all info from "test2" column in the example below

common: "mortalkombat_sonia_rules_abc," player: "Mortal Kombat,"    22-May-22 
Test1   Test2   Type1   Type2   Type3   X   Y   HOR1    VER1    Data1    Error1         
r1107   ab-1    abcr0201    222 22  -222    -22 -222    -22 2   2   Testing     
r1106   ab-1    abcr0201    222 22  -222    -22 -222    -22 2   2   Testing     
c377    ab-1    abcf0402    222 2   -222    -22 -222    -22 2   2   Testing     
r632    ab-1    abcd0402    222 22  -222    -22 -222    -22 2   2   Testing
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Description

^(?!common:)(?:([^s
]+)s+){2}

Regular expression visualization

This regular expression will do the following:

  • Skips the first line that starts with common:
  • Places into capture group 1 the value in the second column
  • Is scalable in that the desired column can be controlled by changing the number in {2} at the end

Example

Live Demo

https://regex101.com/r/rX1dL1/2

Sample text

common: "mortalkombat_sonia_rules_abc," player: "Mortal Kombat,"    22-May-22 
Test1   Test2   Type1   Type2   Type3   X   Y   HOR1    VER1    Data1    Error1         
r1107   ab-1    abcr0201    222 22  -222    -22 -222    -22 2   2   Testing     
r1106   ab-2    abcr0201    222 22  -222    -22 -222    -22 2   2   Testing     
c377    ab-3    abcf0402    222 2   -222    -22 -222    -22 2   2   Testing     
r632    ab-4    abcd0402    222 22  -222    -22 -222    -22 2   2   Testing

Sample Matches

MATCH 1
1.  [87-92] `Test2`

MATCH 2
1.  [176-180]   `ab-1`

MATCH 3
1.  [257-261]   `ab-2`

MATCH 4
1.  [338-342]   `ab-3`

MATCH 5
1.  [419-423]   `ab-4`

Explanation

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
    common:                  'common:'
----------------------------------------------------------------------
  )                        end of look-ahead
----------------------------------------------------------------------
  (?:                      group, but do not capture (2 times):
----------------------------------------------------------------------
    (                        group and capture to 1:
----------------------------------------------------------------------
      [^s
]+                 any character except: whitespace (
,
                               
, 	, f, and " "), '
' (newline)
                               (1 or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
    )                        end of 1
----------------------------------------------------------------------
    s+                      whitespace (
, 
, 	, f, and " ") (1
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
  ){2}                     end of grouping
----------------------------------------------------------------------

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...