正则表达式提取两个不同字符和字符串之间的字符串
简短的问题,但无法使其发挥作用。我有一个字符串:
COMPANY NAME - username (Name Surname).
在这个例子中,什么样的正则表达式会给我 -
和 (
之间的 username
(不带空格)?
它是 ASP. NET C# 如果有什么区别的话,提前致谢
编辑: 公司名称是一个可能包含空格的字符串。用户名不带空格。字符 -
和 (
仅出现在这两个地方。自从我给出了这样的例子以来,我认为这是 100% 明显的。
Short question, but can't make it work. I have a string:
COMPANY NAME - username (Name Surname).
What kind of regex will give me username
(without spaces) from between -
and (
in such example?
It's ASP.NET C# if it makes any difference. Thanks in advance !
EDIT :
The company name is a string with possible spaces. Username is without spaces. The characters -
and (
are present only in these 2 places. I thought it was 100% obvious since I gave such example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您的行以
.
结尾,则正则表达式为@"^.* - (.*?) \(.*\)\.$"
通过使用
.*?
(惰性量词)这个正则表达式对奇怪的“东西”有很强的抵抗力,比如我用作测试的东西。与测试的链接。跳过每一行以查看捕获组。
If your line ends with a
.
then the Regex is@"^.* - (.*?) \(.*\)\.$"
Through the use of
.*?
(the lazy quantifier) this Regex is quite resistant to strange "things" like the one I'm using as a test.Link with tests. Pass over each row to see the capture group.
输出:
用户名
Output :
username
您可以观看下面的 youtube 正则表达式视频,我相信上面的问题您可以自己回答。
http://youtu.be/C2zm0roE-Uc
You can watch the below youtube regular expression video , i am sure the above question you can answer it yourself.
http://youtu.be/C2zm0roE-Uc
使用 grep 测试正则表达式很容易:
It's easy to test regexes with grep: