我如何用正则表达式做到这一点?
我想匹配这个字符串: -myString
但我不想匹配-myString此字符串:--myString
myString当然是什么.
它甚至可能吗?
编辑:
这里有一些关于我到目前为止所得到的信息,因为我发布了一个问题:
string to match:
some random stuff here -string1, --string2, other stuff here
regex:
(-)([\w])*
Run Code Online (Sandbox Code Playgroud)
此正则表达式返回我3场比赛:
-string1,-和-string2
理想情况下,我希望它只返回我的-string1比赛
以下正则表达式是错误的 - 有人可以帮助找到所有:-)前面没有"请忽略我"吗?我之前不需要这样的正则表达式.单词边界可能会混淆事物.
谢谢
<script type="text/javascript">
function regexTest(string, assert) {
document.write('<hr>')
document.write(string)
document.write("[")
document.write(string.match(/\b((?!please ignore me )\:\-\))\b/gi)!=null);
document.write("]?" + assert);
}
regexTest("1. this is the first string :-) ",true);
regexTest("2. :-)",true)
regexTest("3. another string:-)here",true);
regexTest("4. Ending a sentence with :-)",true);
regexTest("5. please ignore me :-)",false);
</script>
Run Code Online (Sandbox Code Playgroud) 假设我有一个字符串"\"Bob \",\"1 \",\"Mary \",\"2 \"".是否可以只删除数字周围的引号而不是字母?我已经尝试了前瞻/后退,但是后视的非可变长度搞砸了我,我不知道如何解决问题.谢谢.
我需要一个python中的正则表达式,匹配任何被2个下划线包围的字符.含义,含义
__a__
Run Code Online (Sandbox Code Playgroud)
将匹配"a",但是
___a___
Run Code Online (Sandbox Code Playgroud)
不会匹配.它需要支持重叠匹配,这样
__a__d___b___e__c__
Run Code Online (Sandbox Code Playgroud)
将返回"ac",因为a被双下划线包围,但d,e在它们旁边有一个三重奏,而b在两侧都有三重下划线.我现在有什么
(?<=[_]{2})(.)(?=[_]{2})
Run Code Online (Sandbox Code Playgroud)
它解决了重叠,但不是上面例子中的"确切2"它返回"adbec"
嗨,我需要匹配cola xx,:ca:cr:pr cola xx但也可以cola xx在没有ca:cr:pr发生时获得.以标签开头的标签数量:可以不同,也可以是它们的长度.
>>> string
':ca:cr:pr cola xx'
>>> re.findall("\w+", string)
['ca', 'cr', 'pr', 'cola', 'xx']
>>> re.findall(":\w+", string)
[':ca', ':cr', ':pr']
>>> re.findall("^(:\w+)", string)
[':ca']
Run Code Online (Sandbox Code Playgroud)
我也试图使用lookbehinds (http://runnable.com/Uqc1Tqv_MVNfAAGN/lookahead-and-lookbehind-in-regular-expressions-in-python-for-regex),但不成功.
>>> re.findall(r"(\s\w+)(?!:)",string)
[' cola', ' xx']
>>> string="cola"
>>> re.findall(r"(\s\w+)(?!:)",string)
[]
Run Code Online (Sandbox Code Playgroud)
那是没有标签的时候,只有cola它没有被检测到.
如何提高我的正则表达式按预期工作?
再一次所需的例子:
:c cola xx - > cola xx
:ca:c cola xx - > cola xx
:ca:cr:pr cola xx - > cola xx
cola …
我有这个使用前向和后向预测的正则表达式:
import re
re.compile("<!inc\((?=.*?\)!>)|(?<=<!inc\(.*?)\)!>")
Run Code Online (Sandbox Code Playgroud)
我正在尝试将其从 C# 移植到 Python,但不断收到错误
look-behind requires fixed-width pattern
Run Code Online (Sandbox Code Playgroud)
是否可以用 Python 重写它而不失去意义?
这个想法是为了让它匹配类似的东西
<!inc(C:\My Documents\file.jpg)!>
Run Code Online (Sandbox Code Playgroud)
更新
我正在使用lookarounds来解析我修改过的HTTP多部分文本
body = r"""------abc
Content-Disposition: form-data; name="upfile"; filename="file.txt"
Content-Type: text/plain
<!inc(C:\Temp\file.txt)!>
------abc
Content-Disposition: form-data; name="upfile2"; filename="pic.png"
Content-Type: image/png
<!inc(C:\Temp\pic.png)!>
------abc
Content-Disposition: form-data; name="note"
this is a note
------abc--
"""
multiparts = re.compile(...).split(body)
Run Code Online (Sandbox Code Playgroud)
我只想在进行分割时获取文件路径和其他文本,而不必删除开始和结束标签
<!inc(代码简洁很重要,但如果可以使正则表达式可行,我愿意更改格式。