miguello
miguello

Reputation: 584

Splitting filename - weird behavior

I'm trying to split a filename like XXX_YYYY-YYYY_fff.xxx

If I'm using

'XXX_YYYY-UUUU_fff.xxx' -split '[-_`.]'

everything is working fine, showing:

XXX
YYYY
UUUU
fff
xxx

But when I'm using

'XXX_YYYY-UUUU_fff.xxx' -split '[_-`.]'

it does not split on dashes:

XXX
YYYY-UUUU
fff
xxx

Can anybody explain why?

Upvotes: 4

Views: 65

Answers (1)

Bill_Stewart
Bill_Stewart

Reputation: 24585

- specifies a range of characters in a regular expression. To get what you want, "escape" the - with \; e.g.:

C:\> 'XXX_YYYY-UUUU_fff.xxx' -split '[_\-.]'
XXX
YYYY
UUUU
fff
xxx

i.e., split the string using any of the following characters: _, -, or ..

You can also tell the regular expression parser that the - is a literal character in the set by placing it at the beginning of the set just after the [ or at the end of the set by placing it just before the ], as in:

[-_.]

or

[_.-]

Upvotes: 5

Related Questions