我需要一个匹配blahfooblah但不匹配的正则表达式blahfoobarblah
我希望它只匹配foo和foo周围的所有东西,只要它没有跟着bar.
我尝试使用它:foo.*(?<!bar)它非常接近,但它匹配blahfoobarblah.背后的负面看法需要匹配任何东西而不仅仅是酒吧.
我正在使用的特定语言是Clojure,它使用Java正则表达式.
编辑:更具体地说,我也需要通过blahfooblahfoobarblah但不是blahfoobarblahblah.
我正试图在R中做一个lookbehind正则表达式来找到一个模式.我希望这会在'bob'中拉出'b',但我得到一个错误.
> regexpr("(?<=a)b","thingamabob")
Error in regexpr("(?<=a)b", "thingamabob") :
invalid regular expression '(?<=a)b', reason 'Invalid regexp'
Run Code Online (Sandbox Code Playgroud)
这不会引发错误,但它也找不到任何错误.
> regexpr("(.<=a)b","thingamabob")
[1] -1
attr(,"match.length")
[1] -1
attr(,"useBytes")
[1] TRUE
Run Code Online (Sandbox Code Playgroud)
我很困惑,因为regexpr的帮助页面明确指出lookbehind应该有效:http://stat.ethz.ch/R-manual/R-patched/library/base/html/regex.html
有任何想法吗?
我希望将标准JSON对象处理为一个对象,其中每行必须包含一个单独的,自包含的有效JSON对象。查看JSON行
JSON_file =
[{u'index': 1,
u'no': 'A',
u'met': u'1043205'},
{u'index': 2,
u'no': 'B',
u'met': u'000031043206'},
{u'index': 3,
u'no': 'C',
u'met': u'0031043207'}]
Run Code Online (Sandbox Code Playgroud)
To JSONL:
{u'index': 1, u'no': 'A', u'met': u'1043205'}
{u'index': 2, u'no': 'B', u'met': u'031043206'}
{u'index': 3, u'no': 'C', u'met': u'0031043207'}
Run Code Online (Sandbox Code Playgroud)
我当前的解决方案是将JSON文件读取为文本文件,并[从开头和]结尾删除。因此,在每行上创建一个有效的JSON对象,而不是在包含行的嵌套对象上创建一个有效的JSON对象。
我想知道是否有更优雅的解决方案?我怀疑在文件上使用字符串操作可能会出错。
目的是json在Spark上将文件读入RDD。查看相关问题- 使用Apache Spark读取JSON-`corrupt_record`
示例:
01 = match
10 = match
99 = match
00 = no match
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有这个:/^ [1-9][0-9] | [0-9][1-9] $/但我觉得它可以优化.
我想创建一个正则表达式,它将采用一到十个数值但如果只提供0则不应接受
例如
1 is valid input
1111123455 is valid input
01 is valid input
010 is valid input
0000 is not valid input
0 is also not valid input
0000000000 is also not valid input
Run Code Online (Sandbox Code Playgroud)
我试过正则表达式
^([0-9]{1,10}|)$
Run Code Online (Sandbox Code Playgroud)
它接受十个数字,但如何只避免0
我正在使用vim中的一个简单文本文件,我希望在完全停止(点/周期)后以2个空格结束每个句子.但是,我不希望那些在完全停止后已经有2个空格的句子进一步增加空格.测试文本可以是:
This sentence has only 1 space after it. This one has two. This line has again 1 space only. This is last line.
Run Code Online (Sandbox Code Playgroud)
我试过了:
%s/\. /\. /g
Run Code Online (Sandbox Code Playgroud)
但是这会将所有空间增加一个.我也试过了,但它不起作用:
%s/\. \\([^ ]\\)/. \\1/g
Run Code Online (Sandbox Code Playgroud)
我怎样才能在vim中实现这一目标?
我搜索并发现[^?]不会包含某个字符,例如本例中的问号,但它似乎包含一个空格,这不是我想要的。这种模式:
\((.*?)\)[^?]
Run Code Online (Sandbox Code Playgroud)
匹配括号中的任何内容,除非最后一个括号后面有一个问号。
(need to capture including brackets) ignore this
(ignore this completely)?
Run Code Online (Sandbox Code Playgroud)
此模式在不包含空格的情况下正确捕获括号中的顶行,但也捕获下面我想忽略的行:
\((.*?)\)
Run Code Online (Sandbox Code Playgroud)
我可以使用什么模式来捕获顶行而没有尾随空格但忽略下面的行?
您可以看到这些模式都不能正常工作: