1

I have this string

AttendanceList
XXXXXX
US\abraham
EU\sarah
US\gerber

when i try to use -replace it replaces all characters inserted in square bracket (including the first line AttendanceList)

$attendance_new = $attendance -replace "[EU\\]", "" -replace"[US\\], ""
echo $attendance_new

AttndancLit
XXXXXX
abraham
arah
grbr

i was hoping to get this sample output (and possibly concatenate a string "_IN" after all values)

AttendanceList
XXXXXX
abraham_IN
sarah_IN
gerber_IN

I'm new to regex and still trying to figure out the regex code for special characters

Tamarah
  • 19
  • 1
  • Please clarify if you want to only match `EU` or `US` at the start of a line followed with a ``\`` char and then remove these values and add `_IN` to the end of these lines. Users [start thinking up](https://stackoverflow.com/questions/70763070/remove-all-values-in-a-string-before-a-backslash/70763115?noredirect=1#comment125099339_70763115) additional requirements or suggesting different logic. – Wiktor Stribiżew Jan 19 '22 at 08:20

1 Answers1

3

You can use

$attendance_new = $attendance -replace '(?m)^(?:US|EU)\\(.*)', '$1_IN'

See this demo (.NET regex demo here). Details:

  • (?m) - multiline option enabling ^ to match start of any line position
  • ^ - line start
  • (?:US|EU) - EU or US
  • \\ - a \ char
  • (.*) - Group 1: any zero or more chars other than a line feed char (note you might need to replace it with ([^\r\n]*) if you start getting weird results)
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 1
    the OP seems to want to remove ANY text up to & including the "\". so your regex pattern seems to be overly specific. i would use something like `'(?m)^.+\\(.+)', '$1'` instead. – Lee_Dailey Jan 18 '22 at 23:43