perl不用新线来串通字符串

发布于 2025-02-13 04:28:34 字数 1144 浏览 0 评论 0原文

我在perl上的字符字符号遇到了这个问题。我的目标是删除所有“”。和“ - ”我的XML。它与前4个标签搭配得很好,但不会删除“”。和“ - ”描述标签上具有新的内容。

这是我的代码:

 />([^\000]*?)</g;
 $lastp = $1;
 $lastp =~ s/[\.|\-]*?//g;
 $_ =~ s/(>)[^\000]*?(<)/$1$lastp$2/g; 

这是我的示例数据:

   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>.44.95......</price>
      <publish_date>200.0-10-
      01.</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>

这是我的输出:

 <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>4495</price>
      <publish_date>200.0-10-
      01.</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
  </book>

I have this problem with subtituting characters on perl. My goal is to remove every "." and "-" on my xml. It goes well with the first 4 tags, but it doesnt remove the "." and "-" on the description tag that has a newline on its content.

This is my code:

 />([^\000]*?)</g;
 $lastp = $1;
 $lastp =~ s/[\.|\-]*?//g;
 $_ =~ s/(>)[^\000]*?(<)/$1$lastp$2/g; 

This is my sample data:

   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>.44.95......</price>
      <publish_date>200.0-10-
      01.</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>

And here is my output:

 <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>4495</price>
      <publish_date>200.0-10-
      01.</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
  </book>

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

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

发布评论

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

评论(1

挽梦忆笙歌 2025-02-20 04:28:34

您能否尝试以下内容:

#!/usr/bin/perl

use strict;
use warnings;

$_ =<<'EOS';
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>.44.95......</price>
      <publish_date>200.0-10-
      01.</publish_date>
      <description>An in-depth look at creating applications
      with XML.</description>
   </book>
EOS

s/(?<=>)[^<]+/ my $s = 
amp;; $s =~ s#[-.]##g; $s /ge;

print;

输出:

   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>4495</price>
      <publish_date>200010
      01</publish_date>
      <description>An indepth look at creating applications
      with XML</description>
   </book>
  • 替换立即作为字符串处理整个行。
  • 模式(?
    零宽度lookbehind主张&gt;[^&lt;]+匹配
    除了&lt;以外的任何字符的顺序。然后分配变量$&amp;
    到匹配的子字符串。
  • 我的$ s = $&amp ;; $ s = 〜s#[ - 。] ## g; $ s删除-
    匹配的子字符串。然后修改的字符串$ s用作
    替换 s/stater/替换/操作员的 E e 选项。

Would you please try the following:

#!/usr/bin/perl

use strict;
use warnings;

$_ =<<'EOS';
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>.44.95......</price>
      <publish_date>200.0-10-
      01.</publish_date>
      <description>An in-depth look at creating applications
      with XML.</description>
   </book>
EOS

s/(?<=>)[^<]+/ my $s = 
amp;; $s =~ s#[-.]##g; $s /ge;

print;

Output:

   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>4495</price>
      <publish_date>200010
      01</publish_date>
      <description>An indepth look at creating applications
      with XML</description>
   </book>
  • The substitution processes the whole lines as a string at once.
  • The pattern (?<=>)[^<]+ matches a string between tags: (?<=>) is a
    zero width lookbehind assertion of leading > and [^<]+ matches the
    following sequence of any characters except for <. Then the variable $& is assigned
    to the matched substring.
  • my $s = $&; $s =~ s#[-.]##g; $s removes - and . from the
    matched substring. Then the modified string $s is used as the
    replacement of the s/pattern/replacement/ operator thanks to the e option.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文