相关疑难解决方法(0)

使用点或逗号作为分隔符,使用或不使用小数的Python正则表达式?

我正在学习正则表达式,现在我正在尝试匹配一个或多或少代表这个的数字:

[zero or more numbers][possibly a dot or comma][zero or more numbers]
Run Code Online (Sandbox Code Playgroud)

没有点或逗号也没关系.所以它应该匹配以下内容:

1
123
123.
123.4
123.456
.456
123,  # From here it's the same but with commas instead of dot separators
123,4
123,456
,456
Run Code Online (Sandbox Code Playgroud)

但它不应与以下内容相匹配:

0.,1
0a,1
0..1
1.1.2
100,000.99  # I know this and the one below are valid in many languages, but I simply want to reject these
100.000,99
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已经提出了[0-9]*[.,][0-9]*,但它似乎没有那么好用:

>>> import re
>>> r = re.compile("[0-9]*[.,][0-9]*")
>>> if r.match('0.1.'): print 'it matches!'
... …
Run Code Online (Sandbox Code Playgroud)

python regex

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

试图用正则表达式查找字母组

我需要在括号中找到一个或多个已定义的字符组.如果存在多个组,则将用连字符分隔.

例:

(us)
(jp)
(jp-us)
(jp-us-eu)
Run Code Online (Sandbox Code Playgroud)

如果字符串只包含一个组,我已经想出如何找到该组:

/\(us\)|\(jp\)/
Run Code Online (Sandbox Code Playgroud)

然而,当我找到不止一个时,我感到困惑,被一个超级分开,没有特别的顺序:(us-jp)OR(jp-us)

任何帮助表示赞赏.

谢谢,西蒙

php regex search find

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

解析hashtags的文本并使用php替换链接

我有一些推特风格#hashtags的文字.我如何编写一个函数来解析可能包含无限数量的#hashtags的文本正文,获取主题标签的文本并将其全部替换为<a href="tag/[hashtag text]">[hashtag text]</a>

我已经考虑了很多关于如何做到这一点,但我真的很难用正则表达式编写这些类型的函数.

示例文字:

Lorem ipsum dolor坐下来,精致的adipistur elit.Vivamus #tristique non elit eu iaculis.Vivamus eget ultricies nisi.在mauris condimentum scelerisque的Vivamus hendrerit.Donec nibh mauris,pulvinar et #commodo a,porta et tellus.Duis eget ante gravida,convallis augue id,blandit lectus.Mauris euismod commodo mi ut fringilla.Sed felis magna,rhoncus vitae mattis varius,sagittis eros.Donec eget porta ipsum.#Mauris sed mauris ante.Suspendisse的潜力.Donec #pretium #augue,eget hendrerit orci.整数诅咒scelerisque consequat.

php regex tags

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

将 \n 替换为 vuejs 上的新行

我正在尝试将 \n 字符替换为来自端点的数据的新行。


我试过了<p>{{ item.licensedocument.legal.documentText.replace(/(?:\r\n|\r|\n)/g, '<br />') }}</p>,没有用。当我将 replace() 写到 prob 的末尾时,大括号停止工作并且永远不会像 JS 一样。输出是这样的:

    {{ item.licensedocument.legal.documentText.replace(/(?:\r\n|\r|\n)/g, '
') }}
Run Code Online (Sandbox Code Playgroud)

当我写的时候<p>{{ item.licensedocument.legal.documentText }}</p>它就工作了,输出就像

GENERAL SERVICE AGREEMENT\n\nTHIS GENERAL SERVICE AGREEMENT (the "Agreement") dated this 22nd day of June, 2018\nBETWEEN:\nCLIENT\n______________________\n______________________________\nCONTRACTOR\n______________________\n______________________________\nBACKGROUND\nThe Client is of the opinion that the Contractor has the necessary qualifications, experience and abilities to provide services to the Client.\nThe Contractor is agreeable to providing such services to the Client on the terms and conditions set out in …
Run Code Online (Sandbox Code Playgroud)

html javascript newline vue.js

9
推荐指数
4
解决办法
1万
查看次数

在正面观察后匹配所有特定角色

我整个早上一直试图让这个正则表达式正确,我已经撞到了墙上.在下面的字符串中,我不想匹配后面的每个正斜杠,.com/<first_word>除了URL / 之后的任何正斜杠.

$string = "http://example.com/foo/12/jacket Input/Output";
    match------------------------^--^
Run Code Online (Sandbox Code Playgroud)

斜杠之间的单词长度无关紧要.

正则表达式:(?<=.com\/\w)(\/)结果:

$string = "http://example.com/foo/12/jacket Input/Output"; // no match
$string = "http://example.com/f/12/jacket Input/Output";   
    matches--------------------^
Run Code Online (Sandbox Code Playgroud)

正则表达式:(?<=\/\w)(\/)结果:

$string = "http://example.com/foo/20/jacket Input/O/utput"; // misses the /'s in the URL
    matches----------------------------------------^
$string = "http://example.com/f/2/jacket Input/O/utput"; // don't want the match between Input/Output
    matches--------------------^-^--------------^                    
Run Code Online (Sandbox Code Playgroud)

因为lookbehind可以没有修饰符并且需要是一个零长度断言,我想知道我是否刚刚错误的路径并且应该寻找另一个正则表达式组合.

这种正面方式是正面的吗?或者我错过了大量咖啡以外的其他东西?

:标记PHP,因为正则表达式应该在工作的任何preg_*功能.

php regex regex-lookarounds

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

如何将管道运算符用作正则表达式的一部分?

我想匹配字符串中的url

u1 = "Check this out http://www.cnn.com/stuff lol"
u2 = "see http://www.cnn.com/stuff2"
u3 = "http://www.espn.com/stuff3 is interesting"
Run Code Online (Sandbox Code Playgroud)

像下面这样的东西,但它很麻烦,因为我必须重复整个模式

re.findall("[^ ]*.cnn.[^ ]*|[^ ]*.espn.[^ ]*", u1)
Run Code Online (Sandbox Code Playgroud)

特别是,在我的真实代码中,我想匹配更多的网站.理想情况下,我可以做类似的事情

re.findall("[^ ]*.cnn|espn.[^ ]*", u1)
Run Code Online (Sandbox Code Playgroud)

但当然它现在不起作用,因为我没有正确指定网站名称.怎么能做得更好?谢谢.

python regex

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

RegEx Challenge:捕获特定行中的所有数字

假设我们有这样的文字:

...
settingsA=9, 4.2 
settingsB=3, 1.5, 9, 2, 4, 6
settingsC=8, 3, 2.5, 1
...
Run Code Online (Sandbox Code Playgroud)

问题是如何使用单个步骤捕获特定行中的所有数字?

单步意味着:

  • 单一的正则表达式.
  • 单次操作(无循环或拆分等)
  • 所有匹配都在一个数组中捕获.

假设我想要捕获以行开头的所有数字settingsB=.最终结果应如下所示:

3
1.5
9
2
4
6
Run Code Online (Sandbox Code Playgroud)

我失败的尝试:

<?php
    $subject =
        "settingsA=9, 4.2
         settingsB=3, 1.5, 9, 2, 4, 6
         settingsC=8, 3, 2.5, 1";

    $pattern = '([\d\.]+)(, )?' // FAILED!
    $pattern = '(?:settingsB=)(?:([\d\.]+)(?:, )?)' // FAILED!
    $pattern = '(?:settingsB=)(?:([\d\.]+)(?:, )?)+' // FAILED!
    $pattern = '(?<=^settingsB=|, )([\d+\.]+)' // FAILED!

    preg_match_all($pattern, $subject, $matches, PREG_SET_ORDER);
    if ($matches) {
        print_r($matches);
    }
?>
Run Code Online (Sandbox Code Playgroud)

更新1: …

php regex text pcre notepad++

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

正则表达式从带有顺序组的分隔字符串解析

我正在尝试从分隔的字符串中解析出单词,并按顺序排列捕获组.例如

dog.cat.chicken.horse.whale

我知道([^.]+)哪个可以解析每个单词但是这会将每个字符串放在捕获组1中.

Match 1
Full match  0-3 `dog`
Group 1.    0-3 `dog`
Match 2
Full match  4-7 `cat`
Group 1.    4-7 `cat`
Match 3
Full match  8-15    `chicken`
Group 1.    8-15    `chicken`
Match 4
Full match  16-21   `horse`
Group 1.    16-21   `horse`
Match 5
Full match  22-27   `whale`
Group 1.    22-27   `whale`
Run Code Online (Sandbox Code Playgroud)

我真正需要的是类似的东西

Match 1
Full match  0-27    `dog.cat.chicken.horse.whale`
Group 1.    0-3 `dog`
Group 2.    4-7 `cat`
Group 3.    8-15    `chicken`
Group 4.    16-21   `horse`
Group 5. …
Run Code Online (Sandbox Code Playgroud)

regex regex-group prometheus

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

问号像什么?:和?!在 Javascript regexp 中是什么意思?

喜欢这个正则表达式吗?它匹配什么?

document.getElementById("MyElement").className = 
   document.getElementById("MyElement").className.replace
  ( /(?:^|\s)MyClass(?!\S)/ , '' )
Run Code Online (Sandbox Code Playgroud)

javascript regex

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

任何人都能解释一下吗?:正则表达式

TCL:任何人都能解释一下吗?:正则表达式

我之间感到困惑?和?:.

?表示前面的字符可能存在,也可能不存在.

然后我不明白是什么(?:)表示.

任何人都可以请解释一下.

([0-9]+(?:\.[0-9]*)?)
Run Code Online (Sandbox Code Playgroud)

regex tcl

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