我有这个正则表达式:^\/\*检查文件是否在开头包含这两个字符.我正在迭代许多c ++源文件,试图看看它们包含哪些.问题是,如果文件包含这个:
#include <source.h>
/* this is a comment */
Run Code Online (Sandbox Code Playgroud)
这也匹配正则表达式.我不明白为什么,因为正则表达式没有多行标志.
这是正则表达式的代码:
multi = /^\/\*/
Run Code Online (Sandbox Code Playgroud)
为什么这个匹配只在文本的开头?这基本上就是我正在做的一切:
data = File.read(filename)
if data =~ multi
puts "file starts with multiline header"
end
Run Code Online (Sandbox Code Playgroud) 我仍然是Scala noob,这让我很困惑:
import java.util.regex._
object NumberMatcher {
def apply(x:String):Boolean = {
val pat = Pattern.compile("\\d+")
val matcher = pat.matcher(x)
return matcher.find
}
def unapply(x:String):Option[String] = {
val pat = Pattern.compile("\\d+")
val matcher = pat.matcher(x)
if(matcher.find) {
return Some(matcher.group())
}
None
}
}
object x {
def main(args : Array[String]) : Unit = {
val strings = List("geo12","neo493","leo")
for(val string <- strings) {
string match {
case NumberMatcher(group) => println(group)
case _ => println ("no")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想为包含数字的字符串添加模式匹配(所以我可以了解更多关于模式匹配的内容),并且unapply …
language-features functional-programming scala pattern-matching
我delayed在David Pollak 看到了一个例子"Beginning Scala".我试图通过反复试验来适应这种情况.这就是我所拥有的:
def sayhello() = {
println("hello")
}
def delaying(t: => Unit):Unit = {
println("before call")
t
println("after call")
}
delaying(sayhello())
Run Code Online (Sandbox Code Playgroud)
你会如何延迟一个带参数的函数/方法?我打电话时为什么不能使用parantheses t?我在哪里可以找到有关延迟功能的更多文档?
假设我有这个:
public class Whatever {
private ArrayList<String> myList = new ArrayList<String>();
// more code goes here
}
Run Code Online (Sandbox Code Playgroud)
或者说我有这个:
public class Whatever {
private ArrayList<String> myList = null;
public Whatever() {
myList = new ArrayList<String>();
}
}
Run Code Online (Sandbox Code Playgroud)
这两个初始化之间的区别是myList什么?预先设置第一个变体是不对的?
我正在尝试用Java编写自己的"功能"小库.如果我有这个功能:
public static <T> List<T> filter(Iterable<T> source,BooleanTest predicate)
{
List<T> results = new ArrayList<T>();
for(T t : source)
{
if(predicate.ok(t))
results.add(t);
}
return results;
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能在这个片段中使用它:
String strings[] = {"one","two","three"};
List<String> containingO = IterableFuncs.filter(strings,new BooleanTest() {
public boolean ok(String obj) {
return obj.indexOf("o") != -1;
}
});
Run Code Online (Sandbox Code Playgroud)
据我所知,Java数组实现了Iterable,对吧?需要更改什么才能使函数与数组以及集合一起使用?通过选择Iterable作为第一个参数,我想我已经涵盖了所有案例.
Where扩展方法必须包含的程序集名称是什么?我在谷歌上找不到它的名字,显然它不是其中之一:
using System;
using System.Collections.Specialized;
using System.Linq.Expressions;
using System.Collections;
using System.Collections.Generic;
Run Code Online (Sandbox Code Playgroud) 是否有任何工具可以将C++代码转换为xml,或者其他一些更容易解析的格式?如果它也可以选择将xml转回C++,那就太好了.我已经知道了doxygen的xml格式......也许它只是我,但我觉得它没有特别的帮助.
假设我的目标之一是:
<path id="files">
<fileset dir="${env.DIRECTORY}" casesensitive="false">
<include name="**/*.html"/>
<exclude name="**/*ant*"/>
</fileset>
</path>
Run Code Online (Sandbox Code Playgroud)
我想将所有html文件分组,除了包含字符串ant的文件.我上面写的方式,它不起作用.我也试过像这样指定排除:
<exclude name="*ant*"/>
Run Code Online (Sandbox Code Playgroud)
请注意,文件集的区分大小写已关闭.但是,如果我写:
<exclude name="**/*ant*/**"/>
Run Code Online (Sandbox Code Playgroud)
这确实有效.为什么第一版和第二版的排除不起作用?
我正在尝试将用户名与正则表达式匹配.请不要建议拆分.
USERNAME=geo
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
String input = "USERNAME=geo";
Pattern pat = Pattern.compile("USERNAME=(\\w+)");
Matcher mat = pat.matcher(input);
if(mat.find()) {
System.out.println(mat.group());
}
Run Code Online (Sandbox Code Playgroud)
为什么不在geo小组中找到?我注意到如果我使用它.group(1),它会找到用户名.但是该group方法包含USERNAME=geo.为什么?
假设我有这两个花车:
a = 50.0
b = 1048576.0
c = a/b
Run Code Online (Sandbox Code Playgroud)
通过打印c,我明白了:
4.76837158203125e-005
Run Code Online (Sandbox Code Playgroud)
进行除法calc.exe给出结果0.0000476837158203125.有没有办法用Ruby实现相同的东西而不安装任何额外的gem?