我需要提取HTTP请求的虚拟主机名.由于这将针对每个请求完成,我正在寻找最快的方法来执行此操作.
以下代码和时间只是我学习的一些方法.
那么,有一些更快的方法来做到这一点?
$hostname = "alphabeta.gama.com";
$iteractions = 100000;
//While Test
$time_start = microtime(true);
for($i=0;$i < $iteractions; $i++){
$vhost = "";
while(($i < 20) && ($hostname{$i} != '.')) $vhost .= $hostname{$i++};
}
$time_end = microtime(true);
$timewhile = $time_end - $time_start;
//Regexp Test
$time_start = microtime(true);
for($i=0; $i<$iteractions; $i++){
$vhost = "";
preg_match("/([A-Za-z])*/", $hostname ,$vals);
$vhost = $vals[0];
}
$time_end = microtime(true);
$timeregex = $time_end - $time_start;
//Substring Test
$time_start = microtime(true);
for($i=0;$i<$iteractions;$i++){
$vhost = "";
$vhost = substr($hostname,0,strpos($hostname,'.'));
}
$time_end …Run Code Online (Sandbox Code Playgroud) 我想转这样的事
CS 240, CS 246, ECE 222, ... (more or less); Software Engineering students only
Run Code Online (Sandbox Code Playgroud)
成
('CS 240', 'CS 246', 'ECE 222', 'ECE 220')
Run Code Online (Sandbox Code Playgroud)
在Python中,匹配单个课程的代码看起来像
>>> re.search('([A-Z]{2,5} \d{3})', 'SE 112').groups()
('SE 112',)
Run Code Online (Sandbox Code Playgroud)
我更喜欢只有正则表达式的方法,因为我有一堆其他的替代reg exps使用'|' 结合它们.但是,分割方法是可以接受的.
这是代码:
> var reg = new RegExp(" hel.lo ", 'g');
>
> var str = " helalo helblo helclo heldlo ";
>
> var mat = str.match(reg);
>
> alert(mat);
Run Code Online (Sandbox Code Playgroud)
它警告"helalo,helclo",但我希望它是"helalo,helblo,helclo,heldlo".只有他们中的一半匹配,我猜这是因为只计算一次的空间.所以我尝试在处理之前将每个空间加倍,但在某些情况下,这还不够.我正在寻找解释和解决方案.
谢谢
我目前正在阅读算法的书,并遇到了稳定的匹配问题.一个问题浮现在脑海里,我很好奇,但这本书没有回答.在每个SMP中,总是可以有一对,每个最喜欢另一个?就像在经典的婚姻例子中一样.是否总有一对有一个女人和一个男人在哪里都排在彼此的首选?
我在F#中写了以下内容:
let fib x =
match x with
| 0 -> 0
| 1 -> 1
| n+2 -> fib n + fib (n+1)
Run Code Online (Sandbox Code Playgroud)
不幸的是,我收到一个编译器错误,说明我在意外的地方使用了一个中缀操作符.除了使用通配符之外,有没有办法在F#中表达我的意图?
可以说我有大量的(随机)文本.在此文本中有一个电话号码,包括三个数字,一个短划线,另外三个数字,一个短划线和四个数字.例如,XXX-XXX-XXXX.从文本中检索此数字的正则表达式是什么?我试过用:
Matcher matcher = pattern.matcher(previousText);
Pattern pattern2 = Pattern.compile(".*(\\d\\d\\d-\\d\\d\\d-\\d\\d\\d\\d).*")
Matcher matcher2 = pattern2.matcher(currentText);
Run Code Online (Sandbox Code Playgroud)
现在,虽然它会起作用,但事实并非如此.请帮忙.
我想在javascript中实现一个匹配文本中一个或多个关键字的正则表达式匹配函数.每个关键字都必须以任何顺序找到.关键字由用户输入.
这是我在的地方:
\b(smart.*\bwork.*|work.*\bsmart.*)
Run Code Online (Sandbox Code Playgroud)
"trying to work smarter""trying to work" or "trying to be smart"这种方法的问题是我必须创建关键字集的不同有序排列(无论如何).
所以问题是:
"\b(smart.*|work.*)<magic:all,anyorder>"什么将确保找到所有关键字,并以任何顺序)[1,2,3]生成[[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]])UPDATE
这旨在用于在客户端(在浏览器中)搜索一组字符串(10-50个字符串).
我有一堆字符串,我想知道它们是否存在于JSON响应中.
我使用了以下代码,但它无法正常工作.我不想循环访问JSON数据.
$url = "https://graph.facebook.com/me/feed?access_token=".$session['access_token'];
$page=file_get_contents($url);
$json_a = json_decode($page,true);
$pos = strpos($page, $mystring);
if($pos == true)
{
do something
}
Run Code Online (Sandbox Code Playgroud)
$ page不包含带有JSON提要内容的字符串.如何将JSON转换为字符串,以便我可以检查$ mystring?
编辑:
这很奇怪.当我使用网址https://graph.facebook.com/me/feed?access_token=".$ session ['access_token']
我得到空数据,但是当我使用"https://graph.facebook.com/me/friends?access_token=".$session['access_token']时;
一切都很好,我得到了所有朋友的名单.我无法理解我哪里出错了?
这是链接http://developers.facebook.com/docs/reference/api/ 我使用了Profile feed(Wall)的确切url格式:https://graph.facebook.com/me/feed?access_token =. ..
与F#进行模式匹配时遇到问题.我正在构建一个F#库,到目前为止这个:
namespace parser
module parse =
let public y = function
| x when x.Contains("hi") -> "HELLO"
| x when x.Contains("hello") -> "HI"
| x -> x
Run Code Online (Sandbox Code Playgroud)
但它给了我错误:根据此程序点之前的信息查找不确定类型的对象的错误.在此程序点之前可能需要类型注释来约束对象的类型.这可以允许解析查找.
我正在尝试模拟常见测试框架(例如JUnit或TestNG)的预期异常行为.
到目前为止,这是我能想到的(工作):
trait ExpectAsserts
{
self : {
def fail (message : String)
def success (message : String)
} =>
def expect[T](exceptionClass : Class[T])(test : => Unit)
{
try
{
test
fail("exception not thrown")
}
catch
{
case expected : T => success("got exception " + expected)
case other : Exception => fail("expected "+ exceptionClass + " but " + other + " thrown instead.")
}
}
}
object Main extends ExpectAsserts
{
def main (args : Array[String])
{
expect(classOf[ArithmeticException]) …Run Code Online (Sandbox Code Playgroud) pattern-matching ×10
regex ×4
algorithm ×2
f# ×2
javascript ×2
php ×2
java ×1
json ×1
proof ×1
python ×1
scala ×1
string ×1
type-erasure ×1