我写了下面的sed one liner来代替IP地址的第3部分.
sed 's/192\.168\.[0-9]*\.\([0-9]*\)/192.168.15.\1/g'
192.168.0.1
192.168.15.1
Run Code Online (Sandbox Code Playgroud)
我希望把它懒惰所以它会取代192.168.0与192.168.15使用??是扩展的正则表达式,但是当我提供"-r"选项,我得到了下面的错误.
$ sed -r 's/192\.168\.[0-9]*\.\([0-9]*\)/192.168.15.\1/g'
sed: -e expression #1, char 44: invalid reference \1 on `s' command's RHS
Run Code Online (Sandbox Code Playgroud)
你能解释一下这里发生了什么以及如何在使用捕获组时使表达变得懒惰吗?
注意:我可以使用许多不同的方法来实现这个例子,我的问题不是如何解决这个例子.我很有兴趣使用带有扩展正则表达式和捕获组的sed.
有没有办法只获得独特的比赛?在匹配后不使用列表或映射,我希望匹配器输出立即是唯一的.
样本输入/输出:
String input = "This is a question from [userName] about finding unique regex matches for [inputString] without using any lists or maps. -[userName].";
Pattern pattern = Pattern.compile("\\[[^\\[\\]]*\\]");
Matcher matcher = pattern.matcher(rawText);
while (matcher.find()) {
String tokenName = matcher.group(0);
System.out.println(tokenName);
}
Run Code Online (Sandbox Code Playgroud)
这将输出以下内容:
[userName]
[inputString]
[userName]
Run Code Online (Sandbox Code Playgroud)
但我希望它输出以下内容:
[userName]
[inputString]
Run Code Online (Sandbox Code Playgroud) 我试图在Scala反射中看到一个注释,到目前为止还没有骰子.我错过了什么?
我的注释:( Java)
@Target({ElementType.PARAMETER}) // Also tried ElementType.CONSTRUCTOR
@Retention(RetentionPolicy.RUNTIME)
public @interface MongoKey {
String info = "";
}
Run Code Online (Sandbox Code Playgroud)
尝试使用Scala反射访问它的部分:
case class One(
@MongoKey name : String,
stuff : List[String]
)
val targetObj = One("FOO", List("a","b"))
val targetType = typeOf[One]
// Given an object (case class) the Type of the case class, and a field name,
// retrieve the typed field object from the case class.
def unpack[T](target: T, t: Type, name: String): (Any, Type) = {
val im = cm.reflect(target)(ClassTag(target.getClass)) …Run Code Online (Sandbox Code Playgroud) 我有一个案例,我想使用文件中的输入作为printf()awk 格式.当我在代码中的字符串中设置它时,我的格式化工作,但是当我从输入加载它时它不起作用.
这是问题的一个小例子:
$ # putting the format in a variable works just fine:
$ echo "" | awk -vs="hello:\t%s\n\tfoo" '{printf(s "bar\n", "world");}'
hello: world
foobar
$ # But getting the format from an input file does not.
$ echo "hello:\t%s\n\tfoo" | awk '{s=$0; printf(s "bar\n", "world");}'
hello:\tworld\n\tfoobar
$
Run Code Online (Sandbox Code Playgroud)
所以...格式替换工作(" %s"),但不是像tab和换行符这样的特殊字符.知道为什么会这样吗?有没有办法"做某事"输入数据,使其可用作格式字符串?
更新#1:
作为进一步的示例,请考虑以下使用bash heretext:
[me@here ~]$ awk -vs="hello: %s\nworld: %s\n" '{printf(s, "foo", "bar");}' <<<""
hello: foo
world: bar
[me@here ~]$ awk '{s=$0; printf(s, "foo", "bar");}' <<<"hello: %s\nworld: …Run Code Online (Sandbox Code Playgroud) 有一些文件夹上有超过100个文件.但所有文件和文件夹名称都被错误的编码名称(UTF-> ANSI)破坏.
"C:\...\?“???????°?‡?°-?›?µ?‡?µ?±???? ????????????\???µ???‚???»?µ?‚???°\?“???????°?‡?°-?›?µ?‡?µ?±???? ????????????\?“???????°?‡?°-?›?µ?‡?µ?±???? ????????????\???µ???‚???»?µ?‚???°\?“???????°?‡?°-?›?µ?‡?µ?±???? ????????????\???µ???‚???»?µ?‚???°\?“???????°?‡?°-?›?µ?‡?µ?±???? ????????????\?“???????°?‡?°-?›?µ?‡?µ?±???? ????????????\?“???????°?‡?°-?›?µ?‡?µ?±???? ????????????\???µ???‚???»?µ?‚???°\?“???????°?‡?°-?›?µ?‡?µ?±???? ????????????\?“???????°?‡?°-?›?µ?‡?µ?±???? ????????????\???µ???‚???»?µ?‚???°\?“???????°?‡?°-?›?µ?‡?µ?±???? ????????????\..."
Run Code Online (Sandbox Code Playgroud)
常规功能Utf8ToAnsi使其完成,但FindFirst无法搜索名称长度超过255个符号的文件夹.
它只给我70/100文件.
我知道这里有很多问题比较一个框架和另一个框架.我觉得我还要补充一点.
Play Framework比Tapestry5框架有什么优势?你会推荐哪一个?为什么?
以下是我发现的相似之处.
为什么选择一个而不是另一个.我曾经用它们来做一个'美化你好世界'类型的应用程序,我觉得两者非常相似.
我试图用一个跨越多行的文本替换一个单词.我知道我可以简单地使用换行符\n来解决这个问题,但我想保持字符串"干净"任何不需要的格式.
以下示例显然不起作用:
read -r -d '' TEST <<EOI
a
b
c
EOI
sed -e "s/TOREPLACE/${TEST}/" file.txt
Run Code Online (Sandbox Code Playgroud)
有关如何实现这一点的任何想法没有修改以读取开始并以EOI结束的部分?
让我们假设我们有一个代表二进制分数的字符串,例如:
".1"
Run Code Online (Sandbox Code Playgroud)
作为十进制数,这是0.5.Python中是否有一种标准方法可以将这些字符串转换为数字类型(无论是二进制还是十进制都不是非常重要).
对于整数,解决方案很简单:
int("101", 2)
>>>5
Run Code Online (Sandbox Code Playgroud)
int()接受一个可选的第二个参数来提供基数,但float()没有.
我正在寻找功能相同(我认为)的东西:
def frac_bin_str_to_float(num):
"""Assuming num to be a string representing
the fractional part of a binary number with
no integer part, return num as a float."""
result = 0
ex = 2.0
for c in num:
if c == '1':
result += 1/ex
ex *= 2
return result
Run Code Online (Sandbox Code Playgroud)
我认为这样做我想要的,虽然我可能错过了一些边缘案例.
是否有内置或标准的方法在Python中执行此操作?
我有一个名为"ips"的文件,其中包含我需要ping的所有ips.为了ping这些IP,我使用以下代码:
cat ips|xargs ping -c 2
Run Code Online (Sandbox Code Playgroud)
但控制台告诉我ping的用法,我不知道如何正确地做到这一点.我正在使用Mac OS