使用正则表达式的字符串操作 [英] String manupulations using Regular Expression

查看:129
本文介绍了使用正则表达式的字符串操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉昨天的问题.

有一个名为"something.docx"的字符串.如何使用正则表达式拆分字符串.

还可以找到如何修改".docx"扩展名,即如何用".pdf"替换它.
如何查找字符串的反义,意味着在"something.docx"中,您可能会从头开始,找到第一个.(点),然后将字符串拆分成那个.(dot),并捕获前面的字符串,意味着"something"并添加".pdf"扩展名.

注意:您将"something.docx"视为字符串,而不是文档,并且也不会转换为pdf格式.只需将.docx替换为.pdf

sry for yesterday''s question..

There is a string named as "something.docx". How to split that string using Regular Expression.

Also find how modify the ".docx" extension,means how it can be replace with ".pdf".
How to find the reverse of string, means In "something.docx", you may come from back onwards, and find the first .(dot) and split the string upto that .(dot) and capture the front string means "something" and add ".pdf" extensions.

Note: You consider "something.docx" as a string, not a document and also its not converted into pdf format. just replace .docx with .pdf

推荐答案

本文 [ ^ ]可以让您简要了解功能强大的正则表达式以及如何使用它们拆分字符串.
This article[^] would give you a brief idea of how powerful regular expressions can be and how they could be used to split strings.


您在这里不需要正则表达式.

查看类System.IO.Path. System.IO.Path.ChangeExtensionSystem.IO.Path.GetExtensionSystem.IO.Path.GetFileNameSystem.IO.Path.GetFileNameWithoutExtension怎么样?它将满足您的所有需求.参见:
http://msdn.microsoft.com/en-us/library/system.io. path.aspx [^ ].

毕竟,您有string.Split.

最后的提示:没有操纵"字符串-它们是不可变的.所有方法仅读取字符串数据并从头开始创建其他数据.实际上,出于性能和其他原因,这是一个很好的了解.

—SA
You don''t need Regular expressions here.

Look at the class System.IO.Path. How about System.IO.Path.ChangeExtension and System.IO.Path.GetExtension, System.IO.Path.GetFileName and System.IO.Path.GetFileNameWithoutExtension? It will cover all you need. See:
http://msdn.microsoft.com/en-us/library/system.io.path.aspx[^].

After all, you have string.Split.

A final note: nothing "manipulates" strings — they are immutable. All methods only read string data and created other data from scratch. Practically, this is good to know, for performance and other reasons.

—SA


这篇关于使用正则表达式的字符串操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆