我有兴趣从长字符串中提取前10个数字而忽略前导零.此外,如果只有零,则只返回1 0,如果没有数字,则返回空字符串.我希望将它与一个匹配find
.
例如:
"abcd00111.g2012asd"
应该匹配 "1112012"
"aktr0011122222222222ddd"
应该匹配 "1112222222"
"asdas000000asdasds0000"
应该匹配 "0"
"adsads.cxzv.;asdasd"
应该匹配 ""
这是我到目前为止所尝试的:Ideone演示 - 代码
Pattern p = Pattern.compile("[1-9]{1}+[0-9]{9}");
Matcher m = p.matcher(str);
if (m.find()) {
String match = m.group();
System.out.println(match);
}
Run Code Online (Sandbox Code Playgroud)
问题是这个正则表达式在第一个非零之后需要9个连续数字,我需要任何9位数(其间可能是非数字字符).
请注意,在我的代码中,if (m.find())
而不是while (m.find())
因为我希望在单次运行中找到匹配.
UPDATE
基于评论我明白,正则表达式不可能在单次运行中完成.
我想答案不一定是基于正则表达式,但最有效率,因为我将多次执行此方法.
我有以下代码(这是用于演示的部分伪代码):
void foo(...){
//some code here
do{
min_item = _MAX_VALUE;
//some code here also
if (min_item == _MAX_VALUE)
break;
if (smaller_item_x == min_item){
FIRST_IS_SMALLER:
global_queue[size++] = smaller_item_x;
if (next_item_x!=0){
smaller_item_x= next_item_x;
if (smaller_item_x > smaller_item_y)
goto SECOND_IS_SMALLER;
}
}else{
SECOND_IS_SMALLER:
global_queue[size++] = smaller_item_y;
if (next_item_y!=0){
smaller_item_y= next_item_y;
if (smaller_item_y > smaller_item_x)
goto FIRST_IS_SMALLER;
}
}
}while(true)
Run Code Online (Sandbox Code Playgroud)
据我所知goto在汇编程序中被转换为jmp,我有兴趣通过将第二个goto更改为类似于branch的命令来增加此过程的性能(较短的命令与短跳转),我可能会遗漏一些东西,它可以是琐碎的,所以我道歉.
我试图将参数传递给python cgi脚本,此参数包含加号运算符.
/cgi-bin/test.py?input=print%20"%20"%20"%20%20-+-\"
Run Code Online (Sandbox Code Playgroud)
python cgi脚本很简单:
#!/usr/bin/python -w
import cgi
fs = cgi.FieldStorage()
print "Content-type: text/plain\n"
strE=fs.getvalue("input")
print strE
Run Code Online (Sandbox Code Playgroud)
我的输出是:
print " " " - -\"
Run Code Online (Sandbox Code Playgroud)
我不明白为什么'+'加运算符被空格替换,我怎么可以通过'+'加运算符?
编辑
@Tom Anderson回答了我的问题,我想再提一下我的问题.
我有一个java脚本函数调用获取带参数的url:
<script type="text/javascript">
function PostContentEditable(editableId,targetOutput)
{
var texthtml = document.getElementById(editableId).innerHTML
var tmp = document.createElement("DIV");
tmp.innerHTML = texthtml ;
var str= tmp.textContent||tmp.innerText;
var xmlhttp;
if (str.length == 0){
document.getElementById(targetOutput).innerHTML = "";
return;
}
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById(targetOutput).innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","../cgi-bin/exercise.py?input="+str,true);
xmlhttp.send();
} …
Run Code Online (Sandbox Code Playgroud) 我正在扩展我以前的问题python高效子字符串搜索,
我有兴趣提高子串搜索实现的性能,
我前一个问题的一些答案指出子串搜索是通过使用受BM算法启发的fastsearch实现的,这里是源代码
更多的答案指向了Boyer-Moore算法,Rabin-Karp算法的python实现.
将c代码作为使用这些算法(BM,Rabin-Karp)的子串搜索的良好实现嵌入是否有效?
c ×2
performance ×2
python ×2
algorithm ×1
c++ ×1
caching ×1
cgi ×1
java ×1
javascript ×1
optimization ×1
regex ×1
substring ×1