正则表达式匹配数值变量名 - Regex matching numerical variable names

- 此内容更新于:2015-12-20
主题:

假设我们有以下字符串:我需要找出如何动态和corrcetly取代这些变量。假设我们有folloving伪代码:但在第一次迭代时,变量被取代,但只有1美元是正确的。有人建议如何修复它吗?

原文:

suppose that we have following string:

$6 
$7 

You have received the grade of $3 in the subject $1 ($10) in the semester $2 ($11). 
Grade date: $4 
Entered: $5 

I need to figure out how to dynamically and corrcetly replace those variables. And suppose we have folloving PL/SQL pseudo-code:

for i in 1..X loop
  l_str := regexp_replace(l_str, '\$'||to_char(i), l_replace(i));
end loop;

But when it comes to first iteration - $1, $10, $11 variables are being replaced - but only $1 is correct.

Does anyone have advice how to fix it?

网友:试一试

(原文:Try l_str := regexp_replace(l_str, '\$[0-9]+\b','SOME_STRING');)

楼主:它不是一个静态的更换,我已经改变的例子,对不起,骗人的把戏

(原文:It is not a static replacement, I've changed example, sorry for mystification)

解决方案:
你的regexp匹配任何开始和当前迭代数量美元。如果需要替换所有这些变量具有相同的字符串,你不需要一个周期,你可以这样做:如果你需要不同的不同的替代品,你同样可以做到:
原文:

Your regexp matches anything starting with $ and a current iteration number. If you need to replace all those variables with the same string, you don't need a cycle there as you can simply do it like that:

l_str := regexp_replace(l_str, '\$[0-9]+','SOME_STRING');

if you need to have different different replacements, you can do it similarly:

for i in 1..X loop
  l_str := regexp_replace(l_str, '(\$'||to_char(i)||')([^0-9]+|$)',l_replace(i)||'\2');
end loop;
楼主:实际上,它应该根据工作:链接,但输出保持不变。我发现“Oracle11g不支持正则表达式的超前。”——也许这是问题吗?

(原文:Actually, it should work according to: link, but the output remains unchanged. What I have found that "Oracle11g does not support Lookahead of Regex yet." - maybe that is the problem?)

网友:哦,对不起。但是你需要用不同的字符串替换var,不一样吗?

(原文:oh, sorry then. But do you need to replace vars with different strings, not the same one?)

楼主:是的,例如,1名学生,5美元应该替换为日期

(原文:Yes, for example, $1 is name of the student, $5 should be replaced with date)

网友:试试这个。我更新了答案。这个想法是为了找到一个var,其余non-digit,然后在把字符串替换和休息

(原文:Try this. I updated the answer. The idea is to find a var AND the rest which is non-digit, then in replacement to put your string AND the rest)

网友:太棒了!它的工作原理,你今天真让我高兴!谢谢你们!

(原文:Awesome! It works, you made my day! Thank you!)