从字符串中删除随机表达式 [英] Remove a random expression from string

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

问题描述

我有一个像这样的字符串/列

I have a string/column something like this

String a = "000003023_AggregateStopLossLimit_W x3A 973911_2012-12-22.PDF";

我想创建一个没有'x3A部分的子字符串973911'在其中。

I want to create a substring which doesn't have the part ' x3A 973911' in it.

这意味着我想要这样的东西,

Whic means I want something like this,

000003023_AggregateStopLossLimit_W_2012-12-22.PDF

有一个列表这些字符串将具有不同的值,但格式将相同。我希望删除字符串的一部分,它在第一个空格之后,在下一个'_'结束。

There is a list of such strings which will have different values but the format will be the same. I want the part of string to be removed which comes after the first space and ends at the next '_'.

这是我已经完成的,这是有效的很好,但想知道是否有更好的方法。

This is what I have done already, this is working fine, but want to know if there is a better way of doing it.

String b = a.replaceAll(a.substring(a.indexOf(" "), a.indexOf("_",a.indexOf(" "))),"");

如果我能在db本身(这是oracle)中执行此操作会更好,而不是java的。有没有想过直接使用select从列中获取这个格式化的字符串?

It would be even better if I can do this in db itself, which is oracle, instead of in java. Any idea to get this formatted string from the column directly using select?

还有一个要求,我不想显示文件的扩展名。
所以在'。'之后什么都不应该显示,这意味着像这样的东西'000003023_AggregateStopLossLimit_W_2012-12-22'

我试过以下使用以前的APC解决方案

One more requirement, I dont want to display the extension of the file.
So nothing after the '.' should be displayed, which means something like this '000003023_AggregateStopLossLimit_W_2012-12-22'
I tried the following using the previous solution of APC

 select regexp_replace ( your_string
                          , '([^[:space]]*) (.*)_(.*)....'
                          , '\1_\3') as new_string from your_table

现在工作正常。
这应该删除最后4个字符,如果扩展名大于或小于3,则存在无法获得正确结果的风险如果字符串没有被截断。
我正在寻找一种更美观的方式来实现它。

有机会吗?

This is working fine for now.
This should be removing last 4 characters and have the risk of not getting proper result if the extension is more or less than 3 or if the string is not truncated.
I'm looking for a more aesthetic way to do it.
Any chance?

推荐答案

要在数据库中执行此操作:

To do it in the database:

select regexp_replace ( your_string
                         , '([^[:space]]*) (.*)_(.*)'
                         , '\1_\3') as new_string
from your_table






不幸的是,Oracle在其正则表达式实现中没有任何强制执行惰性(非贪婪)的语法。这就是为什么我的原始'(。*)'包含 x3A :它与最后一个空格匹配,后面跟下划线。但是,否定语法会将字符串隔离到第一个空格。


Unfortunately Oracle doesn't have any syntax to enforce laziness (non-greediness) in its regex implementation. That's why my original '(.*) ' included the x3A: it matched up to the last space with a following underscore. However, the negation syntax will isolate the string up to the first space.


W缺失后的'_'。有机会获得吗?

"The '_' after W is missing. Any chance to get that also?"

您可以随意格式化替换字符串。简单的方法是做我已经完成的工作,并在两个匹配的模式之间硬编码下划线。或者你可以把它作为一个搜索模式,并将它包含在替换字符串中(尽管你更喜欢用它来做更复杂的搜索)。

You can format the replacement string anyway you want. The easy way out is to do what I have done, and hardcode the underscore between the two matched patterns. Alternatively you could make it a search pattern in its own right and include it in the replacement string (although you're more more likley to do that for more complicated searches).

Oracle在10g中引入正则表达式;这些功能包含在文档中。正则表达式实现符合POSIX,因此它缺少Perl中可能遇到的一些功能。有关正则表达式支持的详细信息,请参见 SQL参考的附录。

Oracle introduced Regular Expressions in 10g; the functions are covered in the documentation. The regex implementation is POSIX compliant, so it lacks some of the functions you might have come across in say Perl. The Regex support is detailed in an appendix to the SQL ref.

至于教程,我有一本翻译过的O'Reilly口袋书。我在Open World 2003获得了我的副本,但电子书的价格合理。 在此处购买。 Anotgher的良好起点是OTN论坛上 cd 的一系列主题:从这里开始阅读

As for tutorials, well I have a much-thumbed copy of the O'Reilly pocket book; I was given my copy at Open World 2003 but the ebook is reasonably priced. Buy it here. Anotgher good starting point is a series of threads by cd on the OTN forum: start reading here.

这篇关于从字符串中删除随机表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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