使用字符串作为模式,但不将其删除
我对正则表达式还很陌生,我顺便学到了一些东西,但仍然是知识!
所以我想请您澄清它是如何工作的!
假设我有以下字符串,正如您所看到的,它们的格式可能略有不同,但它们非常相似!
DTSTART;TZID="America/Chicago":20030819T000000
DTEND;TZID="America/Chicago":20030819T010000
DTSTART;TZID=US/Pacific
DTSTART;VALUE=DATE
现在我想替换第一个AZ块和冒号之间的所有内容,例如我会保留我
DTSTART:20030819T000000
DTEND:20030819T010000
DTSTART
DTSTART
已经掌握的菜鸟知识这个该死的正则表达式! :-(
preg_replace( '/^[A-Z](?!;[A-Z]=[\w\W]+):$/m' , '' , $data );
但为什么我确定这个正则表达式不起作用!?:-)
请帮助我!
PS:问题的标题已经解释得很清楚了,我还想知道如何使用众所周知的字符串块来匹配另一个...
preg_replace( '/^[DTSTART](?!;[A-Z]=[\w\W]+):$/m' , '' , $data );
..无需删除 DTSTART
谢谢您的时间!
问候 卢卡·菲洛索菲
i'm pretty new on regex, i have learned something by the way, but is still pour knowledge!
so i want ask you for clarification on how it work!
assuming i have the following strings, as you can see they can be formatted little different way one from another but they are very similar!
DTSTART;TZID="America/Chicago":20030819T000000
DTEND;TZID="America/Chicago":20030819T010000
DTSTART;TZID=US/Pacific
DTSTART;VALUE=DATE
now i want replace everything between the first A-Z block and the colon so for example i would keep
DTSTART:20030819T000000
DTEND:20030819T010000
DTSTART
DTSTART
so on my very noobs knowledge i have worked out this shitty regex! :-(
preg_replace( '/^[A-Z](?!;[A-Z]=[\w\W]+):$/m' , '' , $data );
but why i'm sure this regex will not work!? :-)
Pls help me!
PS: the title of question is pretty explaned, i want also know how for example use a well know string block for match another...
preg_replace( '/^[DTSTART](?!;[A-Z]=[\w\W]+):$/m' , '' , $data );
..without delete DTSTART
Thanks for the time!
Regards
Luca Filosofi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用相对简单的正则表达式,如下所示。
它在行的开头查找一系列大写字母,将匹配起点(这就是
\K
所做的)重置为这些字母的末尾,并匹配除冒号或换行符之外的任何内容(即您想要删除的部分)。然后,这些匹配的部分将被替换为空字符串。上面的输出将是
如果您感兴趣的行仅以
DTSTART
或DTEND
开头,那么我们可以更准确地了解要匹配的内容(例如 < code>^DT(?:START|END)) 但[AZ]
显然涵盖了这两者。You could use a relatively simple regex like the following.
It looks for a series of capital letters at the start of a line, resets the match starting point (that's what
\K
does) to the end of those and matches anything not a colon or newline (i.e. the parts you want to remove). Those matched parts are then replaced with an empty string.The output from the above would be
If the lines that you are interested in will only ever start with
DTSTART
orDTEND
then we could be more precise about what to match (e.g.^DT(?:START|END)
) but[A-Z]
obviously covers both of those.如果您想在替换中保留部分匹配模式,请在其两侧加上括号,然后通过 $1 (或任何分组)引用它。
例如:
给出“这是一个句子”
If you want to retain part of the matched pattern in a substitution, you put parentheses around it and then refer to it by $1 (or whichever grouping it is).
For example:
gives "this is a sentence"
您可以查看此示例,其工作方式与您的问题类似,
\w+): (?P\d+)/', $str, $matches);
/* This also works in PHP 5.2.2 (PCRE 7.0) and later, however
* the above form is recommended for backwards compatibility */
// preg_match('/(?\w+): (?\d+)/', $str, $matches);
print_r($matches);
?>
因此如果您只需要数字,则需要打印 $matches[digit]
You can check out this example work similarly as your problem
\w+): (?P\d+)/', $str, $matches);
/* This also works in PHP 5.2.2 (PCRE 7.0) and later, however
* the above form is recommended for backwards compatibility */
// preg_match('/(?\w+): (?\d+)/', $str, $matches);
print_r($matches);
?>
so if u need only digit u need to print $matches[digit]
您想要删除分号和冒号或行尾之间的所有内容,对吧?所以用它作为你的表达方式。你把事情搞得太复杂了。
这是一个非常简单的表达。匹配
(?:;.+?:)
或(?:;.+?$)
,它们仅在终止符上有所不同(第一个匹配最多冒号,第二个匹配到行尾)。每个都是一个非捕获组,以分号开头,勉强读入所有字符,然后在终止符处停止。根据您的描述,与此匹配的所有内容都可以移除。
You want to remove everything between a semicolon and either a colon or the end of the line, right? So use that as your expression. You're overcomplicating things.
It's a pretty simple expression. Either match
(?:;.+?:)
or(?:;.+?$)
, which differ only by their terminator (the first one matches up to a colon, the second one matches up to the end of the line).Each is a non-capturing group that starts with a semicolon, reluctantly reads in all characters, then stops at the terminator. Everything matched by this is removable according to your description.