Perl 解析具有一个或多个字段的字符串

发布于 2024-12-18 03:10:09 字数 760 浏览 0 评论 0原文

我有一个需要解析的字符串。它满足以下要求:

  • 它由0个或多个键->值对组成。
  • 密钥始终是 2 个字母。
  • 该值是一个或多个数字。
  • 键和值之间不会有空格。
  • 各个对之间可能有也可能没有空格。

我可能看到的示例字符串:

  • AB1234 //一个键->值对(键=AB,值=1234)
  • AB1234 BC2345 //两个键->值对,用空格分隔
  • AB1234BC2345 //两个键->值对,不以空格分隔
  • //Empty Sting,没有键->值对
  • AB12345601BC1234CD1232PE2343 //很多键->值对,没有空格
  • AB12345601 BC1234 CD1232 PE2343 //很多键->值对,有空格

我需要构建这个字符串的Perl哈希。如果我能保证它是一对,我会做这样的事情:

$string =~ /([A-Z][A-Z])([0-9]+)/
$key = $1
$value = $2
$hash{$key} = $value

对于多个字符串,我可能会做一些事情,在上述正则表达式的每次匹配之后,我获取原始字符串的子字符串(排除第一个匹配),然后搜索再次。然而,我确信有一种更聪明的、perl 风格的方法来实现这一点。

希望我没有这么糟糕的数据源来处理——

乔纳森

I have a string I need to parse. It meets the following requirements:

  • It is comprised of 0 or more key->value pairs.
  • The key is always 2 letters.
  • The value is one or more numbers.
  • There will not be a space between the key and value.
  • There may or may not be a space between individual pairs.

Example strings I may see:

  • AB1234 //One key->value pair (Key=AB, Value=1234)
  • AB1234 BC2345 //Two key->value pairs, separated by space
  • AB1234BC2345 //Two key->value pairs, not separated by space
  • //Empty Sting, No key->value pairs
  • AB12345601BC1234CD1232PE2343 //Lots of key->value pairs, no space
  • AB12345601 BC1234 CD1232 PE2343 //Lots of key->value pairs, with spaces

I need to build a Perl hash of this string. If I could guarantee it was 1 pair I would do something like this:

$string =~ /([A-Z][A-Z])([0-9]+)/
$key = $1
$value = $2
$hash{$key} = $value

For multiple strings, I could potentially do something where after each match of the above regex, I take a substring of the original string (exempting the first match) and then search again. However, I'm sure there's a more clever, perl-esque way to achieve this.

Wishing I didn't have such a crappy data source to deal with-

Jonathan

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

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

发布评论

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

评论(3

不疑不惑不回忆 2024-12-25 03:10:09

在具有全局标志的列表上下文中,正则表达式将返回 所有匹配的子字符串

use Data::Dumper;

@strs = (
    'AB1234',
    'AB1234 BC2345',
    'AB1234BC2345',
    '',
    'AB12345601BC1234CD1232PE2343',
    'AB12345601 BC1234 CD1232 PE2343'
);

for $str (@strs) {
    # The money line
    %parts = ($str =~ /([A-Z][A-Z])(\d+)/g);

    print Dumper(\%parts);
}

为了获得更大的不透明度,请删除模式匹配周围的括号:%parts = $str =~ /([AZ][AZ])(\d+)/g;

In a list context with the global flag, a regex will return all matched substrings:

use Data::Dumper;

@strs = (
    'AB1234',
    'AB1234 BC2345',
    'AB1234BC2345',
    '',
    'AB12345601BC1234CD1232PE2343',
    'AB12345601 BC1234 CD1232 PE2343'
);

for $str (@strs) {
    # The money line
    %parts = ($str =~ /([A-Z][A-Z])(\d+)/g);

    print Dumper(\%parts);
}

For greater opacity, remove the parentheses around the pattern matching: %parts = $str =~ /([A-Z][A-Z])(\d+)/g;.

世界如花海般美丽 2024-12-25 03:10:09

你已经在那里了:

$hash{$1} = $2 while $string =~ /([[:alpha:]]{2})([0-9]+)/g

You are already there:

$hash{$1} = $2 while $string =~ /([[:alpha:]]{2})([0-9]+)/g
一袭白衣梦中忆 2024-12-25 03:10:09

假设您的字符串肯定会与您的方案匹配(即不会有任何 A122ABC123 形式的字符串),那么这应该有效:

my @strings = ( 'AB1234', 'AB1234 BC2345', 'AB1234BC2345' );

foreach my $string (@strings) {
    $string =~ s/\s+//g;
    my ( $first, %elems ) = split(/([A-Z]{2})/, $string);
    while (my ($key,$value) = each %elems) {
        delete $elems{$key} unless $key =~ /^[A-Z]{2}$/;
        delete $elems{$key} unless $value =~ /^\d{4}$/;
    }
    print Dumper \%elems;
}

Assuming your strings are definitely going to match your scheme (i.e. there won't be any strings of the form A122 or ABC123), then this should work:

my @strings = ( 'AB1234', 'AB1234 BC2345', 'AB1234BC2345' );

foreach my $string (@strings) {
    $string =~ s/\s+//g;
    my ( $first, %elems ) = split(/([A-Z]{2})/, $string);
    while (my ($key,$value) = each %elems) {
        delete $elems{$key} unless $key =~ /^[A-Z]{2}$/;
        delete $elems{$key} unless $value =~ /^\d{4}$/;
    }
    print Dumper \%elems;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文