# How to extract Table data using regular expressions?

For example, the Table have follows structure:

Data = {
{userid , 1 , brith , 24},
{math , 90, art , 96, sport, 72 , econmic , 98},
{userid , 2 , brith, 23},
{math , 80 , art , 86, sport , 92 , econmic , 92},
{userid , 3, brith , 25},
{math , 90 , art, 76 , sport , 82 , econmic, 99},
{userid, 1, phonenumber , 9157678481},
{country, UK},
{userid , 2, phonenumber, 9237678481},
{country , USA}
}


My question is how to use regular expressions to extract useid, 1 's data.

Expect result is

{{userid , 1 , brith , 24},
{math , 90, art , 96, sport, 72 , econmic , 98},
{userid, 1, phonenumber , 9157678481},
{country, UK}}


• how about Join @@ Select[#[[1, 2]] == 1 &]@Split[Data, #[[1]] == userid &]?
– kglr
Dec 1, 2021 at 9:41
• Here is another possibility using pattern: Flatten[Cases[Partition[Data, 2], {{userid, 1, __}, {__}}], 1] Dec 1, 2021 at 10:00
• All works, thanks to all comments!
– lumw
Dec 1, 2021 at 11:48
• I see, __ is the BlankSequence.
– lumw
Dec 1, 2021 at 12:32

## SequenceCases

SequenceCases[data, p : {{_, 1, __}, _} :> Sequence @@ p]


## Split + Select

Join @@ Select[#[[1, 2]] == 1 &] @ Split[data, #[[1]] == userid &]


## Split + Cases

Cases[p : {{_, 1, __}, __} :> Sequence @@ p] @ Split[data, #[[1]] == userid &]


• Owesome! How did you do that! SequenceCases is very helpful. if I want remove the expect result like {{userid , 1 , brith , 24}, {math , 90, art , 96, sport, 72 , econmic , 98}, {userid, 1, phonenumber , 9157678481}, {country, UK}} from Data Table, which method can be choose? just some tips very be helpful!
– lumw
Dec 1, 2021 at 13:24
• @Ben, try SequenceCases[data, p : {{userid, Except[1], __}, _} :> Sequence @@ p] or DeleteCases[data, Alternatives @@ SequenceCases[data, p : {{_, 1, __}, _} :> Sequence @@ p]]
– kglr
Dec 1, 2021 at 13:43
• Ace! Thank you so much!
– lumw
Dec 1, 2021 at 13:57