使用字符串作为模式,但不将其删除

发布于 2024-08-30 19:12:56 字数 834 浏览 4 评论 0 原文

我对正则表达式还很陌生,我顺便学到了一些东西,但仍然是知识!

所以我想请您澄清它是如何工作的!

假设我有以下字符串,正如您所看到的,它们的格式可能略有不同,但它们非常相似!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

枉心 2024-09-06 19:12:56

您可以使用相对简单的正则表达式,如下所示。

$subject = 'DTSTART;TZID="America/Chicago":20030819T000000
DTEND;TZID="America/Chicago":20030819T010000
DTSTART;TZID=US/Pacific
DTSTART;VALUE=DATE';

echo preg_replace('/^[A-Z]+\K[^:\n]*/m', '', $subject) . PHP_EOL;

它在行的开头查找一系列大写字母,将匹配起点(这就是 \K 所做的)重置为这些字母的末尾,并匹配除冒号或换行符之外的任何内容(即您想要删除的部分)。然后,这些匹配的部分将被替换为空字符串。

上面的输出将是

DTSTART:20030819T000000
DTEND:20030819T010000
DTSTART
DTSTART

如果您感兴趣的行仅以 DTSTARTDTEND 开头,那么我们可以更准确地了解要匹配的内容(例如 < code>^DT(?:START|END)) 但 [AZ] 显然涵盖了这两者。

You could use a relatively simple regex like the following.

$subject = 'DTSTART;TZID="America/Chicago":20030819T000000
DTEND;TZID="America/Chicago":20030819T010000
DTSTART;TZID=US/Pacific
DTSTART;VALUE=DATE';

echo preg_replace('/^[A-Z]+\K[^:\n]*/m', '', $subject) . PHP_EOL;

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

DTSTART:20030819T000000
DTEND:20030819T010000
DTSTART
DTSTART

If the lines that you are interested in will only ever start with DTSTART or DTEND then we could be more precise about what to match (e.g. ^DT(?:START|END)) but [A-Z] obviously covers both of those.

没有你我更好 2024-09-06 19:12:56

如果您想在替换中保留部分匹配模式,请在其两侧加上括号,然后通过 $1 (或任何分组)引用它。

例如:

s/^(this is a sentence) to edit/$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:

s/^(this is a sentence) to edit/$1/

gives "this is a sentence"

找回味觉 2024-09-06 19:12:56

您可以查看此示例,其工作方式与您的问题类似,

\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);

?>

The above example will output:

Array
(
    [0] => foobar: 2008
    [name] => foobar
    [1] => foobar
    [digit] => 2008
    [2] => 2008
)

因此如果您只需要数字,则需要打印 $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);

?>

The above example will output:

Array
(
    [0] => foobar: 2008
    [name] => foobar
    [1] => foobar
    [digit] => 2008
    [2] => 2008
)

so if u need only digit u need to print $matches[digit]

夏夜暖风 2024-09-06 19:12:56

您想要删除分号和冒号或行尾之间的所有内容,对吧?所以用它作为你的表达方式。你把事情搞得太复杂了。

preg_replace('/(?:;.+?:)|(?:;.+?$)/m','',$data);

这是一个非常简单的表达。匹配 (?:;.+?:)(?:;.+?$),它们仅在终止符上有所不同(第一个匹配最多冒号,第二个匹配到行尾)。

每个都是一个非捕获组,以分号开头,勉强读入所有字符,然后在终止符处停止。根据您的描述,与此匹配的所有内容都可以移除。

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.

preg_replace('/(?:;.+?:)|(?:;.+?$)/m','',$data);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文