小编Pao*_*olo的帖子

R正则表达式:http匹配

我在使用正则表达式匹配http链接时遇到问题.我有一个模式,我想从网站源代码中提取.源代码有200多行,有很多HTML乱码</html><body... useless links useless images'

我需要的http链接属于这种模式:

<a href"http:www.google.com/....1,1">
<a href"http:www.google.com/....2,2">
<a href"http:www.google.com/....3,3">
Run Code Online (Sandbox Code Playgroud)

我只想获得http链接,它们的独特模式就是结局.请帮助,我已经坚持了几个小时试验gusb,regxpr和grep.

regex r

2
推荐指数
1
解决办法
405
查看次数

sapply 2矢量

假设我有2个列表

divisor = c(0, 1, 1, 7, 7, 8, 8, 8, 9 )
remainder = c(99,  0,  1,  1, 99,  0,  1, 99,  0)
Run Code Online (Sandbox Code Playgroud)

如果相应的余数不是0,我想要一个divisor 元素元素+ 1.最终的答案应如下所示:

updated.divisor = (1, 1, 2, 8, 8, 8, 9, 9, 9)
Run Code Online (Sandbox Code Playgroud)

我该如何使用sapply

到目前为止我有

sapply(remainder, function(x) {
    if x != 0{
       #divisor = divisor + 1
    }
    else{
       #divisor = divisor + 0
    }
}
Run Code Online (Sandbox Code Playgroud)

PS我可能使用嵌套循环,但我希望能够使用它sapply.

r

1
推荐指数
2
解决办法
169
查看次数

捕获每行的前n个字符

我在捕获每行的前6个字符时遇到麻烦。假设我在text.txt中包含以下几行:

this is a sentence
1234567890
string
Run Code Online (Sandbox Code Playgroud)

我想获得前10个字符:

this i
123456
string
Run Code Online (Sandbox Code Playgroud)

我试过跑步 grep -Eo "^[a-zA-Z0-9]*{6}" text.txt

但是grep会在第6个位置循环使用正则表达式,而不是从下一行开始。它最终返回:

this i
s a se
ntence
123456
7890..
Run Code Online (Sandbox Code Playgroud)

我在这里做错了什么?

regex grep

1
推荐指数
1
解决办法
5812
查看次数

这个函数的Big-O是什么,可以反转字符串中的单词

我有一个简单的函数,reverseWords(),它隐藏了字符串中的单词.例如.输入S ="这是一个字符串"给出"siht si a gnirts"的输出

我想知道这个功能的大O是什么.是O(N),O(N ^ 2)还是O(N*M)?

def reverseWords(S):
    # code in Python 2.7
    listA = S.split()
    output = []
    for element in listA:
        output.append(element[::-1])

    return (" ".join(output))
Run Code Online (Sandbox Code Playgroud)

是O(N),O(N ^ 2)还是O(N*M)?

  • O(N ^ 2)因为我们有一个嵌套循环,例如.传递输入一次,外部循环,然后将每个字符反转为内部循环
  • O(N*M)与上述相同的原因除了M表示为字符上的循环
  • O(N)因为...我忘记了解释

python big-o time-complexity

1
推荐指数
1
解决办法
938
查看次数

正则表达式:加号与星号

星号或星号告诉引擎尝试匹配前面的标记零次或多次。加号告诉引擎尝试匹配前面的标记一次或多次。

根据定义,我想知道为什么加号比星号返回更多匹配。

 echo "ABC ddd kkk DDD" | grep -Eo "[A-Z]+"
Run Code Online (Sandbox Code Playgroud)

返回

美国广播公司

 echo "ABC ddd kkk DDD" | grep -Eo "[A-Z]*"
Run Code Online (Sandbox Code Playgroud)

返回 ABC

regex grep

0
推荐指数
1
解决办法
1555
查看次数

标签 统计

regex ×3

grep ×2

r ×2

big-o ×1

python ×1

time-complexity ×1