简体   繁体   English

正则表达式使用 Perl 在变量中捕获特定的 8 位字符串

[英]Regex to Capture Specific 8-digit String in Variable using Perl

In my Perl script, I have a variable that contains a specific file path.在我的 Perl 脚本中,我有一个包含特定文件路径的变量。 I need to create a regular expression that can capture a specific 8-digit string from that variable.我需要创建一个可以从该变量中捕获特定 8 位字符串的正则表达式。

When $file_path = "/home/attachments/00883227/sample.txt I want to capture the string of numbers immediately following "attachments".$file_path = "/home/attachments/00883227/sample.txt我想捕获紧跟在“附件”之后的数字字符串。

My (unsuccessful) attempt:我的(失败)尝试:

if($file_path =~ /attachments\/(\d{1,2,3,4,5,6,7,8}+)/)
    { $number = $1; }

When I run this script, though, it looks like nothing is stored in the $number variable.但是,当我运行此脚本时, $number 变量中似乎没有存储任何内容。 The solution for this is probably very simple?对此的解决方案可能非常简单? Please pardon my ignorance, I am very new to Perl.请原谅我的无知,我对 Perl 很陌生。

You don't need to give so much of numbers in the braces. 您不需要在括号中输入太多数字。 Simply use {8} to enforce matching of 8 digits. 只需使用{8}来强制匹配8位数字。 And since you have / inside your string, you can use a different delimiter, instead of escaping the slashes: 并且由于字符串中包含/ ,因此可以使用其他定界符,而不用转义斜杠:

if($file_path =~ m!attachments/(\d{8})!)
   { $number = $1; }

Close, just use (\\d{8}) , like: 关闭,只需使用(\\d{8}) ,例如:

$file_path =~ /attachments\/(\d{8})\b/

Also added \\b so that it doesn't capture any longer numbers. 还添加了\\b ,以使其不再捕获任何数字。

If you want to match exactly 8 digits, just use \\d{8} : 如果要精确匹配8位数字,只需使用\\d{8}

if($file_path =~ /attachments\/(\d{8})/)
    { $number = $1; }
my ($number) = ( $file_path =~ m{ (attachments/( [0-9]{8} ) }x );

Using pattern delimiters other than / such as m{ } , you avoid the so-called leaning toothpick syndrome caused by the need to escape and / characters that appear in the pattern. 使用/以外的模式定界符,例如m{ } ,可以避免由于需要转义和/出现在模式中的字符而导致的所谓的“ 倾斜牙签综合症”

By assigning to $number in list context, the captured substring goes into $number immediately. 通过在列表上下文中分配$number ,捕获的子字符串立即进入$number

By using the x option, you make your pattern somewhat more readable. 通过使用x选项,您可以使图案更具可读性。

Try using: 尝试使用:

if($file_path =~ /attachments\/(\d+)/)
{ $number = $1; }

{ , } is used to limit the number of times a certain character (or group of characters) to repeat. { , }用于限制某个字符(或一组字符)重复的次数。 {n,m} means that the character (or group) should repeat at least n times and at most m times. {n,m}表示字符(或组)应重复至少n次,最多重复m次。

If you're certain the string of digits is 8-digits long, you then use: 如果确定数字字符串的长度为8位数字,则可以使用:

if($file_path =~ /attachments\/(\d{8})/)
{ $number = $1; }

{ } (without commas) will match exactly the number specified. { } (不带逗号)将完全匹配指定的数字。

my ($number) = $file_path =~ m{attachments/(\d+)};

如果您要确保长度恰好是八位数,

my ($number) = $file_path =~ m{attachments/(\d{8})(?!\d)};

Simply give the limits.简单地给出限制。

like, \\d{3,8} where it will return digits between 3-8 length.像, \\d{3,8} 它将返回 3-8 长度之间的数字。

Is it going to be exactly 8 digits or between 1 to 8 digits? 它是正好是8位数字还是1到8位数字?

Since you're looking at /attachments/ as a piece of the string, you probably don't want to use the standard /../ delimiters. 由于您将/attachments/视为字符串的一部分,因此您可能不想使用标准的/../分隔符。 Maybe switching to m{..} or m#..# : 也许切换到m{..}m#..#

if ( $file_path =~ m#/attachments/\(d{1,8})/# ) {

That will capture between 1 to 8 digits. 这将捕获1到8位数字。 To capture exactly 8: 要精确捕获8个:

my $number;
if ( $file_path =~ m#/attachments/(\d{8})/# ) {
   $number = $1;
   ...
}
else {
    ....
}

Note that I define $digit_string before the if statement. 请注意,我在if语句之前定义了$digit_string This way, it's in scope after the if statement (and inside the if statement. (You are using use strict; ? Right?) 这样,它就位于if语句之后的范围内(并且位于if语句内部。(您正在使用use strict;对吗?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM