正则表达式将开始标记和结束标记匹配为新行

发布于 2024-10-27 12:31:04 字数 469 浏览 1 评论 0原文

我一直在制作一个信件编译系统(以在填写问卷后节省人们的时间),并且在项目接近尾声时我们发现了一个错误。长话短说,如果没有这个正则表达式,修复将需要很多小时 - 这就是为什么我请求您的帮助!

我们有一些文本包含以下内容...

"<k>This is the start of the paragraph

This is some more of the paragraph.

And some more";

我基本上需要一个正则表达式来搜索开始标记“”,以及它遇到的第一个新行“\ r\n”?然后将内容插入到我可以使用的变量中(删除了 但新行代码“\r\n”保留在原处)。

我使用的是 PHP,文本(如上面的示例)存储在 MySQL 中。

请帮忙!

我保证在修复这个错误后我会好好学习这些! :)

I've been producing a letter compilation system (to save people time after a questionaire has been filled in) and near the end of the project we've found a bug. Long story short it would take many hours to fix without this regular expression - which is why I'm asking for your fine help!

We have some text that contains the following...

"<k>This is the start of the paragraph

This is some more of the paragraph.

And some more";

I basically need a regular expression that can search for the opening tag, "<k>", and also the first new line it comes across "\r\n"? and then insert the contents into a variable I can then use (with the <k> removed but the new line codes, "\r\n", left in place).

I'm using PHP and the text (like the example above) is stored in MySQL.

Please help!

I promise I'll learn these properly after I've fixed this bug! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

蝶舞 2024-11-03 12:31:04

如果您使用的是 5.3,您可以使用这样的闭包:

$text = "<k>This is the start of the paragraph

This is some more of the paragraph.

And some more";

$matches = array();

$text = preg_replace_callback('/<k>(.*)$/m', function($match) use (&$matches){
    $matches[] = $match[1];
}, $text);

var_dump($text,$matches);

输出是:

string '

This is some more of the paragraph.

And some more' (length=52)
array
  0 => string 'This is the start of the paragraph' (length=34)

我假设文本中可能有多个 标记,因此,我将所有文本跟随标签进入一个名为 matches 的数组。

作为进一步的示例...具有以下输入:

$text = "<k>This is the start of the paragraph
Line 2 doesn't have a tag...
This is some more <k>of the paragraph.
Line 4 doesn't have a tag...
<k>And some more";

输出为:

string '
Line 2 doesn't have a tag...
This is some more 
Line 4 doesn't have a tag...
' (length=78)
array
  0 => string 'This is the start of the paragraph' (length=34)
  1 => string 'of the paragraph.' (length=17)
  2 => string 'And some more' (length=13)

If you are using 5.3 you can make some use of closures like this:

$text = "<k>This is the start of the paragraph

This is some more of the paragraph.

And some more";

$matches = array();

$text = preg_replace_callback('/<k>(.*)$/m', function($match) use (&$matches){
    $matches[] = $match[1];
}, $text);

var_dump($text,$matches);

Output is:

string '

This is some more of the paragraph.

And some more' (length=52)
array
  0 => string 'This is the start of the paragraph' (length=34)

I'm assuming there could be multiple <k> tags in the text, and therefore, I put all the text that follows the tag into an array called matches.

As a further example... with the following input:

$text = "<k>This is the start of the paragraph
Line 2 doesn't have a tag...
This is some more <k>of the paragraph.
Line 4 doesn't have a tag...
<k>And some more";

The output is:

string '
Line 2 doesn't have a tag...
This is some more 
Line 4 doesn't have a tag...
' (length=78)
array
  0 => string 'This is the start of the paragraph' (length=34)
  1 => string 'of the paragraph.' (length=17)
  2 => string 'And some more' (length=13)
难忘№最初的完美 2024-11-03 12:31:04
/^<k>(\w*\s+)$/

可能会起作用。

/^<k>(\w*\s+)$/

would probably work.

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