简体   繁体   English

连接正则表达式s + w +…perl

[英]Concatenate regex s+ w+ … perl

I have entries like that : 我有这样的条目:

    XYZABC------------HGTEZCW
    ZERTAE------------RCBCVQE

I would like to get just HGTEZCW and RCBCVQE . 我只想得到HGTEZCW和RCBCVQE。 I would like to use a generic regex. 我想使用通用正则表达式。

$temp=~ s/^\s+//g;     (1)
$temp=~ s/^\w+[-]+//g; (2)

If i use (1) + (2) , it works. 如果我使用(1)+(2),那就可以了。 It works i get : HGTEZCW , then RCBCVQE ... 我得到它的作品: HGTEZCW ,然后RCBCVQE ...

I would like to know if it is possible to do that in one line like : 我想知道是否可以在一行中做到这一点,例如:

$temp=~ s/^\s+\w+[-]+//g; (3)

When I use (3), i get this result : XYZABC------------HGTEZCW 当我使用(3)时,我得到以下结果: XYZABC------------HGTEZCW

I dont understand why it is not possible to concat 1 + 2 in one line. 我不明白为什么不可能将1 + 2并排成一行。

Sorry my entries was : 抱歉,我的输入是:

    XYZABC------------HGTEZCW
    ZERTAE------------RCBCVQE

Also, the regex 1 remove space but when i use regex2, it remove XYZABC------------ . 另外,正则表达式1删除空间,但是当我使用regex2时,它删除了XYZABC ------------。 But the combination (3), don't work. 但是组合(3)无效。 i have this XYZABC------------HGTEZCW 我有这个XYZABC------------HGTEZCW

@Tim So there always is whitespace at the start of each string? @Tim所以每个字符串的开头总是有空格? yes

Your regex (1) removes whitespace from the start of the string. 您的正则表达式(1)从字符串开头删除空格。 So it does nothing on your example strings. 因此,它对示例字符串没有任何作用。

Reges (2) removes all alphanumerics from the start of the string plus any following dashes, returning whatever follows the last dash. Reges(2)删除字符串开头的所有字母数字以及后面的所有破折号,并返回最后一个破折号之后的内容。

If you combine both, the regex fails because there is no whitespace \\s+ could match - therefore the entire regex fails. 如果将两者结合使用,则正则表达式将失败,因为没有空格\\s+可以匹配-因此整个正则表达式将失败。

To fix this, simply make the whitespace optional. 要解决此问题,只需将空白设为可选。 Also you don't need to enclose the - in brackets: 另外,您也不需要将-括在方括号中:

$temp=~ s/^\s*\w+-+//g;

This should do the trick. 这应该可以解决问题。

$Str = '
    XYZABC------------HGTEZCW
    ZERTAE------------RCBCVQE
';

@Matches = ($Str =~ m#^.+-(\w+)$#mg);

print join "\n",@Matches ;

如果只需要每个条目的最后七个字符,则可以执行以下操作:

$temp =~ /.{7}$/;

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

相关问题 正则表达式:找到“^ \\ w + \\ s +”后面的单词 - 新语法定义(MMIX) - Sublime Text 3 - Regex: find words following on “^\w+\s+” - New Syntax Definition (MMIX) - Sublime Text 3 正则表达式(/&lt;(\\w+)\\s+(.*?)&gt;/)需要改进 - Regular expression (/<(\w+)\s+(.*?)>/) need improvement 为什么/ \\ w +:/和/ \\ S +:/处理回溯不同? - Why do /\w+:/ and /\S+:/ handle backtracking differently? RegExp必须具有\\ w +和\\ s +字符 - RegExp must have \w+ and \s+ characters 正则表达式的区别:(\\ w +)? 和(\\ w *) - Regex difference: (\w+)? and (\w*) 为什么正则表达式\ w *(\ s + | $)找到“foo”(Java)的2个匹配项? - Why does the regex \w*(\s+|$) finds 2 matches for “foo” (Java)? 用正则表达式\ w \ w *拆分字符串? \ w +? - Split String with regex \w \w*? \w+? 正则表达式中的(\\ /?)的含义是(\\ w +)([^&gt;] *?)冗余? - meaning of (\/?) in regex / is (\w+)([^>]*?) a redundancy? 我正在使用正则表达式进行电子邮件验证EMAIL_PATTERN:/ ^(\\ w +([。] \\ w +)* @ \\ w +([。] \\ w +)* \\。\\ w +([。] \\ w +)*)$ /, - I am using Regex for email validation EMAIL_PATTERN:/^(\w+([.]\w+)*@\w+([.]\w+)*\.\w+([.]\w+)*)$/, 怎么说(\\ w + \\ W +)乘以正则表达式4(R gsub) - How to say (\w+\W+) times 4 in regex (R gsub)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM