PHP Preg_match添加一个特殊字符

时间:2017-10-03 15:27:28

标签: php

我想搜索特殊字符连字符' - '和美元' $'在我的字符串中作为下划线我们总是在我的字符串中相同。 string可以是大写或小写 它应该匹配' - '接下来的2个单词和4个数字以及Dollar' $' 但我正在弄乱特殊角色Hyphen' - '美元' $'并得到警告 有什么帮助吗?

#!/usr/bin/env php
<?php

$line = "ESFEDTH-ZD0099$";
if(!preg_match("(^[a-zA-Z0-9]*)-([a-zA-Z]{2})([0-9]{4})", $line)) {
    echo "A match was not found.";
} else {
    echo "A match was found.";
}
?>

结果错了: 警告:preg_match():未知的修饰符&#39; - &#39;在第8行的/Users/skull/Desktop/php/test.php中 找到了一场比赛。

2 个答案:

答案 0 :(得分:1)

你的问题谈到了下划线,但这个例子并没有包含一个。

此外,看起来你的逻辑已经回到了前面。 preg_match()如果匹配则返回1,但是您正在写#34;未找到匹配&#34;如果有比赛。

最后你需要划定正则表达式。

import time
from tkinter import *

bits = 12

def get_coinbase_price():
    global bits
    bits += 1
    thelabel.config(text = "1 BTC = %s USD" % bits)
    root.after(1000, get_coinbase_price)

total = 42

def get_nicehash_stats():
    global total
    total += 1
    secondlabel.config(text="Nicehash stats = %s " % total)
    root.after(1000, get_nicehash_stats)

# gui workspace
root = Tk()
thelabel = Label(root, text="")
secondlabel = Label(root, text="")
thelabel.grid(column=0, row=0)
secondlabel.grid(column=1, row=0)
root.after(1000, get_coinbase_price)
root.after(900, get_nicehash_stats)
root.mainloop()

答案 1 :(得分:-2)

你必须使用\来转义特殊字符。所以如果你想要scape _你必须写\ _

问候。