简体   繁体   English

正则表达式匹配除字符以外的所有内容

[英]regex to match everything except character

I have a payload that contains the following: 我有一个包含以下内容的有效负载:

\\ p a t r i c k - t e s t - f i l e . t x t x SMB2 。 xMB2

I'm looking to extract the file name of patrick-test-file.txt 我正在寻找提取patrick-test-file.txt的文件名

I can get close by using this, but it continues to include everything (including ascii characters) 我可以通过使用它来接近,但是它继续包含所有内容(包括ascii字符)

[\\\\](.*?)x�SMB2

With a result of this: p a t r i c k - t e s t - f i l e . t x t for the capture group. 结果是: 为捕获组。

How would I just match the characters of the file name, which could be anything of variable length, and could contain alphanumeric characters? 我如何匹配文件名中的字符(可以是可变长度的任何内容,并且可以包含字母数字字符)? Is this possible with pure regex? 纯正则表达式可能吗?

Any help is much appreciated. 任何帮助深表感谢。

Sometimes you just can't do a single language-agnostic Regular Expression to accomplish something. 有时,您只是无法执行与语言无关的正则表达式来完成某些任务。 And sometimes (usually) it is more performant to do a series of string functions. 有时(通常)执行一系列字符串函数的性能更高。

I wouldn't personally accept any solution which has hard-coded values, such as x SMB2 . 我个人不会接受任何具有硬编码值的解决方案,例如x SMB2

If you want to use Regular Expressions only, you can first select the File-Name portion like so: (([-\\w\\d.\\\\]+)[^-\\w\\d.\\\\]?)+ , then go ahead and replace [^-\\w\\d.\\\\] with nothing "" . 如果只想使用正则表达式,则可以首先选择File-Name部分,如下所示: (([-\\w\\d.\\\\]+)[^-\\w\\d.\\\\]?)+ ,然后继续将[^-\\w\\d.\\\\]替换为""

Honestly, given the limited detail, the best function is like so: 老实说,鉴于细节有限,最佳功能如下:

var fileName = "\patrick-test-file.txt";

But half-joking aside, and with that limited detail, your best bet is to do a couple string functions: 但是开个玩笑,而且细节有限,您最好的选择是做几个字符串函数:

var yuckyString = @"����\�p�a�t�r�i�c�k�-�t�e�s�t�-�f�i�l�e�.�t�x�t������x�SMB2";
var fileNameArea = yuckyString.Split(new[] { "��" }, StringSplitOptions.RemoveEmptyEntries)[0];
var fileName = fileNameArea.Replace("�", "");

Granted, there was no language listed, so I'm using C#. 当然,没有列出语言,所以我正在使用C#。 Also, the answer would change if there were irregularities with those special characters. 同样,如果那些特殊字符存在不规则之处,答案也会改变。 With the limited info, the pattern seems clear. 在信息有限的情况下,模式似乎很清晰。

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

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