-1

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
Alan Moore
  • 73,866
  • 12
  • 100
  • 156
  • OK; so what have you tried, and why didn't it work, and what result did you get, and what result did you expect, and which language are you using to embed your regexes in? There really isn't enough information here for us to be able to help you. It's interesting to contemplate whether you want any info from the first or second lines in the output. Could there be more than two lines to skip at the start (and if so, how would you know when to start processing the columns)? – Jonathan Leffler Jun 15 '16 at 00:43
  • I have spent days on this and havent been able to get anything matched, i am using both online regular expression testers and expresso application. i have seen some examples like this \s*(?[^,]*) \s*(?[^,]*) ^From:\s*"[^<"]*\Kexample i tried looking at other pages and rewriting their code to match mine. http://stackoverflow.com/questions/6109882/regex-match-all-characters-between-two-strings http://stackoverflow.com/questions/4736/learning-regular-expressions I expected the lower expression and other pages i visited to return between abc "0201 222" -222 – user3570765 Jun 15 '16 at 01:14
  • 1
    You've got a decent answer. Translated to Perl: `perl -nl -e 'print $1 if /^(?!common:)(?:([^\s\n]+)\s+){2}/' data` it produces what you probably want. – Jonathan Leffler Jun 15 '16 at 01:47

1 Answers1

3

Description

^(?!common:)(?:([^\s\n]+)\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\n]+                 any character except: whitespace (\n,
                               \r, \t, \f, and " "), '\n' (newline)
                               (1 or more times (matching the most
                               amount possible))
----------------------------------------------------------------------
    )                        end of \1
----------------------------------------------------------------------
    \s+                      whitespace (\n, \r, \t, \f, and " ") (1
                             or more times (matching the most amount
                             possible))
----------------------------------------------------------------------
  ){2}                     end of grouping
----------------------------------------------------------------------
Ro Yo Mi
  • 14,790
  • 5
  • 35
  • 43