Link to home
Start Free TrialLog in
Avatar of bndit
bndit

asked on

Powershell regex

Hello,

I have the following txt file:

TEAMHAT1 at TEAMHAT2
TEAMHAT3 at TEAMHAT4
TEAMHAT5 vs TEAMHAT6

I want to split at "at" and "vs" and place each team on its own line, like this:

TEAMHAT1
TEAMHAT2
TEAMHAT3
...

In the sample code attached, if I use "at" in the split operator, it splits each line whenever it finds "at"...which is really not what I want...I want to split at the lowercase "at" not at the "AT" within the team's name. If I then try "\w at" it kinda works but then it trims the last character of the first line..like this:

TEAMHAT (missing the digit "1" in the team's name)
TEAMHAT2

I know I'm missing something simple...not so familiar with regular expressions patters but my logic would be to:

Look at the line...find "at" or "vs"...if found...split it into a new line and process the next line

$arr = @()
 
$lines = get-content c:\teams.txt
 
foreach ($line in $lines){
 
	$words = $line -split "at"
 
	foreach ($word in $words){
 
		$arr += $word.trim()
	}
}

$arr

Open in new window

Avatar of Dale Harris
Dale Harris
Flag of United States of America image

Why don't you try splitting it from the spaces:

$arr = gc c:\teams.txt | %{$_.split(" ")[0,2]}


HTH,

Dale Harris
Avatar of bndit
bndit

ASKER

Because some of the team names have spaces....such as "TEAMHAT1 St." ...."TEAMHAT2 WEST"....
Well that throws a loop into things doesn't it.  Do you mind putting down all the possible combinations?  I'm sure I'll come up with something.

DH
Avatar of bndit

ASKER

TROY at FLA INTERNATIONAL
CONNECTICUT at PITTSBURGH
RICE at HOUSTON
VIRGINIA at MIAMI
BYU vs TCU
S CAROLINA ST at HOWARD
BOWLING GREEN at KENT ST
VMI at THE CITADEL
ALBANY at WAGNER
WYOMING at SAN DIEGO ST
IOWA at MINNESOTA
WAKE FOREST at N CAROLINA
MICHIGAN ST at NEBRASKA
ILLINOIS at PENN ST
SACRED HEART at ROBERT MORRIS
W VIRGINIA at RUTGERS
BALL ST at W MICHIGAN
MISSISSIPPI ST at KENTUCKY
NORTHWESTERN at INDIANA
SYRACUSE at LOUISVILLE
UAB at MARSHALL
BOSTON COLLEGE at MARYLAND
PURDUE at MICHIGAN
VIRGINIA TECH at DUKE
NC STATE at FLORIDA ST
CLEMSON at GEORGIA TECH
C MICHIGAN at AKRON
OLE MISS at AUBURN
ARKANSAS at VANDERBILT
KANSAS at TEXAS
YALE at COLUMBIA
FURMAN at CHATTANOOGA
HAMPTON at SAVANNAH ST
AIR FORCE at NEW MEXICO
DAYTON at VALPARAISO
S DAKOTA ST at MISSOURI ST
MCNEESE ST at SF AUSTIN
WEBER ST at MONTANA
NAVY at NOTRE DAME
MISSOURI at TEXAS A&M
IOWA ST at TEXAS TECH
SMU at TULSA
TULANE at E CAROLINA
BUFFALO at MIAMI OHIO
W KENTUCKY at LA MONROE
FORDHAM at ARMY
GEORGIA vs FLORIDA
ALABAMA A&M at ALABAMA ST
COLORADO at ARIZONA ST
TENNESSEE TECH at JACKSONVILLE ST
MEMPHIS at UCF
SAN JOSE ST at LOUISIANA TECH
PORTLAND ST at E WASHINGTON
HAWAII at IDAHO
BUCKNELL at LAFAYETTE
DELAWARE at TOWSON
COLORADO ST at UNLV
OREGON ST at UTAH
CALIFORNIA at UCLA
ARIZONA at WASHINGTON
S CAROLINA at TENNESSEE
BAYLOR at OKLAHOMA ST
WASHINGTON ST at OREGON
OKLAHOMA at KANSAS ST
NORTH TEXAS at ARKANSAS ST
LA LAFAYETTE at MIDDLE TENN ST
WISCONSIN at OHIO ST
NEVADA at NEW MEXICO ST
SOUTHERN MISS at UTEP
STANFORD at USC
ALCORN ST at SOUTHERN
COASTAL CAROLINA at STONY BROOK
N CAROLINA A&T at NORFOLK ST
RHODE ISLAND at NEW HAMPSHIRE
IDAHO ST at MONTANA ST
E KENTUCKY at MURRAY ST
W CAROLINA at SAMFORD
CORNELL at PRINCETON
MASSACHUSETTS at RICHMOND
MAINE at VILLANOVA
ELON at WOFFORD
W ILLINOIS at YOUNGSTOWN ST
PRESBYTERIAN at LIBERTY
DRAKE at MARIST
MORGAN ST at DELAWARE ST
DARTMOUTH at HARVARD
GEORGETOWN at HOLY CROSS
LEHIGH at COLGATE
E ILLINOIS at AUSTIN PEAY
PENNSYLVANIA at BROWN
ASKER CERTIFIED SOLUTION
Avatar of prashanthd
prashanthd
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bndit

ASKER

thx! that was simple....I was stuck trying to come up with a fancy regex filter to find the "at" and "vs" but by using your suggestion it splits at the correct spots...great!
Avatar of bndit

ASKER

Thanks, simple.
Avatar of Qlemo
Just for the files: The accepted solution is overcomplicated, and not very PowerShellish.
   get-content c:\teams.txt -split " at | vs "
is all you need.
Avatar of bndit

ASKER

@Qlemo....tried your way, but it's not working
split.png
Sorry, need to enclose get-content in parens (I was testing with a list of string literals instead of a file):
   (get-content c:\teams.txt) -split " at | vs "

Open in new window

Avatar of bndit

ASKER

ok, that did the trick...very elegant solution....thx