简体   繁体   English

从文件路径中删除动态用户名

[英]Remove dynamic username from file path

I have the following in C#. 我在C#中有以下内容。

string fileName = @"X:\Users\username\AppData\Local\foo.txt";

I want to perform a Regex.Replace so that I get the following back. 我想执行一个Regex.Replace以便获得以下信息。

string fileName = @"X:\Users\AppData\Local\foo.txt";

It would be nice to use a Regex that allows for the drive letter ( X: ) to be dynamic. 使用允许驱动器号( X: :)动态的正则表达式会很好。 In essence, I just need to drop the ' username ' part. 本质上,我只需要删除username部分。 Also, note that username is just a placeholder for the user's actual name - so that could be bob or larry or john or anything else. 另外,请注意,用户名只是用户实际名称的占位符-因此可以是bob或larry或john或其他任何名称。 The Regex will need to take this into account. 正则表达式将需要考虑到这一点。

The thing that you can count on here is that it will always start with X:\\Users\\ (where X: can be C: or D: , etc.) and then will be followed with some string and then will be followed by a \\ and then some other path elements that are not important. 您可以在这里指望的是,它总是以X:\\Users\\开头(其中X:可以是C:D:等),然后跟着一些字符串,然后跟着一个\\ ,然后是其他一些不重要的路径元素。 Also, it would be good to have a case-insensitive match on users or Users . 此外,最好在users or Users上设置不区分大小写的匹配项。

I know that this can be done without Regex , but I need this to fit into a larger system that only provides Regex support. 我知道无需Regex即可完成此操作,但我需要将其装入仅提供Regex支持的更大系统中。 There's no support for things like String.Replace or String.Join . 不支持String.ReplaceString.Join类的东西。

You can try this sample: 您可以尝试以下示例:

string fileName = @"X:\Users\username\AppData\Local\foo.txt";
Console.Write(Regex.Replace(fileName, @"([A-Z]{1}\:*\\Users\\)(\w+\\)(.*)", "$1$3"));

Explanation: 说明:

  1. ([AZ]{1}\\:\\\\[Uu]sers\\\\) - this part of Regex is a first group of characters, before the username , in the path ([AZ]{1}\\:\\\\[Uu]sers\\\\) -正则表达式的这一部分是路径中username之前的第一组字符
    • ^ stands for the start of the line (remove this to ignore words preceding the filename) ^代表行的开头(将其删除以忽略文件名前面的单词)
    • [AZ]{1}\\:\\\\ stands for the disk root path. [AZ]{1}\\:\\\\代表磁盘根路径。 You can choose the disk letters you want to be here, like this: [CDX-Z]{1} . 您可以选择要在此处的磁盘字母,例如: [CDX-Z]{1} Note {1} - this is constraint means you need only 1 uppercase letter. 注意{1} -这是约束条件,意味着您仅需要1个大写字母。
    • [Uu]sers\\\\ stands for the users directory name, note the group for the case-insensitive users path This is first group, refered as $1 in the replace pattern [Uu]sers\\\\代表用户目录名,请注意不区分大小写的用户路径的组这是第一个组,在替换模式中称为$1
  2. (\\w+\\\\) - this part of Regex is a second group of characters, the username , in the path (\\w+\\\\) -正则表达式的这一部分是路径中的第二个字符组,即username
    • \\w+\\\\ stands for at least one word character and \\ sign. \\w+\\\\代表至少一个单词字符和\\符号。 This is a second group, which is not in a replace pattern 这是第二组,未处于替换模式
  3. (.*) - this part of Regex is a third group of characters, after the username , in the path (.*) -正则表达式的此部分是路径中username之后的第三组字符
    • .* stands for any character after the removed \\ symbol. .*代表删除的\\符号后的任何字符。

Update: In the real world I found usernames with spaces, unicode chars and ~ in it. 更新:在现实世界中,我发现用户名中包含空格,unicode字符和〜。 So I replaced the 2. group to include any characters but the \\ sign. 因此,我替换了2.组以包括除\\符号之外的任何字符。

string fileName = @"X:\Users\username\AppData\Local\foo.txt";
Console.Write(Regex.Replace(fileName, @"([A-Z]{1}\:\\[Uu]sers\\)([^\\]*\\)(.*)", "$1$3"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM