如何替换正则表达式中的转义字符?

发布于 2024-08-09 02:58:15 字数 1769 浏览 2 评论 0原文

我再一次碰壁了。

如何使用正则表达式替换转义字符? 如果制表符 (\t) 出现两次以上,我想用单个 \t 替换这两次或多次出现。 例如,如果 \t\t\t 出现,那么我只想将其替换为 \t 。 如何做到这一点?

我还面临一个关于读取文本文件并向其应用正则表达式的问题。

我正在使用 C# 来读取文本文件和正则表达式。 当我打开文本文件(具有 txt 扩展名的文件)时,我会得到文件的正常视图。但是当我使用“textReader”读取同一文件并将其存储到字符串中时,我得到如下文本:

确定\t\t\t\t\t\t电子邮件:
[电子邮件受保护] \r电话:+ 91
992\t\r\r经验总结
\r商业智能和数据
仓库设计师6人以上
多年 OLAP 工作经验
项目。\r\r\r技术\r下面是
重要软件产品列表
以及我使用过的工具
与。\r\r软件
产品\r\a\r\a操作
系统:\rWINDOWS NT、WINDOWS 2000、
UNIX\r数据库管理
系统:\rOracle 8i、Oracle 9i、Oracle
10g、SQL-Server 7.0、DB2\r软件
软件包:\rVSS、ER Win、M1\r​​Fourth
生成语言:\rPL/SQL,
SQL*PLUS\r工具&
技术:\rOracle 仓库
生成器 10.1.0.4.0,ORACLE 9i AS,
ORACLE Discoverer 报告数据阶段
8.0、Fast Track 8.5、DB@ Cube、JavaScript、JSP、JDEV、BI BEANS、ASP、
ASP.NET,Ab
初始\r\r\a\r\a\v\r\r\f作业\r
各种作业的详细信息
我处理过的都列在这里,
按时间顺序
订单。\r\r名称\r\a艾利丹尼森数据
仓储\r\a\r\aClient\r\aAvery
丹尼森,他在项目中挑战
是从现有的 EDW 中获取
拥有数据的仓库
聚合
级别。\r\a\r\a\r\r名称\r\aAOL BI
(Omniture)\rite 团队。设计,
编码和测试
与现场团队协调。
\r\a\r\a工具&技术\r\aUnix
平台,Oracle 10g,Py。不仅
提供了正确的要求,但是
性能也必须在
可以接受
范围。\r\a\r\a\r\r\r\r\r\r\r\r\r\r\r\r名称\r\aAIW
事件 (ABSA)\r\a\r\aClient\r\aABSA,
南非\r\a\r\aP

即所有转义字符如\s、\r、\f 都是可见的。因此,当我将相同的文本读入字符串变量时,适用于普通文本的正则表达式不起作用。

有人知道如何解决这个问题吗?

谢谢

,我还有一个疑问。我想匹配行尾的文本。我尝试为此使用 $ 。例如,为了匹配以“赋值”结尾的文本,我使用了正则表达式赋值$。它适用于普通文本。但是当我对 Streamreader 给出的文本运行此正则表达式时,此正则表达式不起作用。流阅读器给出像Assignments\r\r\f这样的字符串。如何将行尾或行首与这种文本匹配?

Once again I have hit the wall.

How to replace escape characters using regular expressions?
If tab character (\t) occures more than twice, I want to replace those two or more occurances by single \t.
For example if \t\t\t comes, then I want to replace it with \t only.
How to do this?

I am facing one more problem regarding reading text file and applying regular expressions to it.

I am using C# to read text file and for regular expressions.
When I open text file (file having txt extension), I get a normal view of file. But when I read the same file using "textReader" and store it into string, I get text something like this :

O K\t\t\t\t\t\tEmail:
[email protected] \rPhone: + 91
992\t\r\rExperience Summary
\rBusiness Intelligence and data
warehouse designer with more than 6
years of work experience in OLAP
Project.\r\r\rTechnology\rBelow is a
list of important software products
and tools that I have worked
with.\r\rSoftware
Products\r\a\r\aOperating
Systems:\rWINDOWS NT, WINDOWS 2000,
UNIX\rDatabase Management
Systems:\rOracle 8i, Oracle 9i, Oracle
10g, SQL-Server 7.0, DB2\rSoftware
Packages:\rVSS, ER Win, M1\rFourth
Generation Language:\rPL/SQL,
SQL*PLUS\rTools &
Technologies:\rOracle Warehouse
Builder 10.1.0.4.0, ORACLE 9i AS,
ORACLE Discoverer Reports Data Stage
8.0, Fast Track 8.5, DB@ Cube, JavaScript, JSP, JDEV, BI BEANS, ASP,
ASP.NET, Ab
Initio\r\r\a\r\a\v\r\r\fAssignments\rThe
details of the various assignments
that I have handled are listed here,
in chronological
order.\r\rName\r\aAvery Dennison Data
Warehousing\r\a\r\aClient\r\aAvery
Dennison, he challenge in the project
is to feed EDW from existing
warehouses which has data at an
aggregated
level.\r\a\r\a\r\rName\r\aAOL BI
(Omniture)\rite team. Designing,
coding and testing along with
coordination with Onsite team.
\r\a\r\aTools & Technologies\r\aUnix
Platform, Oracle 10g , Py. Not only
delivering the correct requirement but
also the performance has to be in
acceptable
range.\r\a\r\a\r\r\r\r\r\r\r\r\r\r\r\rName\r\aAIW
Events (ABSA)\r\a\r\aClient\r\aABSA,
South Africa\r\a\r\aP

i.e, all the escape characters like \s, \r, \f are visible. Because of this, the regular expression that works with normal text doesnt work when I read same text into string variable.

Anyone one knows how to solve this problem?

Thanks

I have one more query. I want to match text at the end of the line. I tried to use $ for this. For example, to match text ending with "assignment", I used regex assignment$.It worked with normal text. But when I run this regx on text given by streamreader, this regex doesnt work. Stream reader gives strings like Assignments\r\r\f.How to match end of line or start of line with this kind of text?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眸中客 2024-08-16 02:58:15
/\t{2,}/\t/

用单个字符替换两个或多个制表符。

/\t{2,}/\t/

replaces two or more tabs with a single character.

独闯女儿国 2024-08-16 02:58:15

您正在尝试匹配字符串 "\r",对吧?您必须转义转义字符才能执行此操作:

"(\\r)*"

此表达式将匹配 "\r" 任意次数。它之所以有效,是因为 "\\" 转义为文字 "\"。您也可以应用相同的想法来匹配 "\t"

You're trying to match the string "\r", right? You'll have to escape the escape character to do it:

"(\\r)*"

This expression will match "\r" any number of times. It works because "\\" escapes to a literal "\". You can apply the same idea to match "\t", too.

荆棘i 2024-08-16 02:58:15

对于制表符,请使用类似以下内容:

/(\t)*/\1/g
  1. 用一个字符(制表符)创建一组并尽可能匹配它。
  2. 将完整匹配替换为单个字符
  3. (全局),使用全文模式。

然后,您可以对要替换的其他转义字符使用相同的表达式。

For the tab char use something like this:

/(\t)*/\1/g
  1. Make a group with one char (the tab char) and match it as much as possible.
  2. Replace the full match with the single character
  3. (Global) use the pattern for the full text.

Then you could use the same expression for the other escaped chars you want to replace.

困倦 2024-08-16 02:58:15

您可以将 \\t\\t\\t 替换为 \\t{3}

You could replace \\t\\t\\t with \\t{3}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文