简体   繁体   English

如何使用正则表达式删除/ 1或/ 2?

[英]How can I use regex to remove /1 or /2?

Regex gurus, 正则表达式专家

Here is the following line of code I want to parse with regex: 这是我要使用正则表达式解析的以下代码行:

@ERR030882.2595 HWI-BRUNOP16X_0001:3:1:6649:5175#0/1

I want to obtain the following: 我想获得以下内容:

@ERR030882.2595 HWI-BRUNOP16X_0001:3:1:6649:5175#0

I have written the following regex on rubular.com: 我在rubular.com上写了以下正则表达式:

(@.* *.)(!?(\/.))

My idea is to use negation to remove /1 by (!?(\\/.)) . 我的想法是使用否定符通过(!?(\\/.))删除/ 1。 However, this produces the entire line? 但是,这会产生整条生产线吗?

@ERR030882.2595 HWI-BRUNOP16X_0001:3:1:6649:5175#0/1

Why is (?!thisismystring) not removing /1? 为什么(?!thisismystring)无法删除/ 1? I googled the fire out of this, but they seemed to suggest similar things I am already trying? 我用这个来搜索,但他们似乎暗示了我已经在尝试的类似事情? I deeply appreciate your help. 非常感谢您的帮助。

I think what you are trying to write is /(\\@.* .*)(?=\\/\\d)/ (you need to escape the at sign @ to prevent Perl from treating it as an array) but you need a positive look-ahead because you want to match everything up until the following characters are a slash followed by a digit. 我认为您要编写的是/(\\@.* .*)(?=\\/\\d)/ (您需要转义at符号@以防止Perl将其视为数组),但是您需要正向超前,因为您要匹配所有内容,直到以下字符是一个斜杠后跟一个数字。

Here is a program that demonstrates. 这是一个演示程序。

use strict;
use warnings;
use 5.010;

my $s = '@ERR030882.2595 HWI-BRUNOP16X_0001:3:1:6649:5175#0/1';

$s =~ /(\@.* .*)(?=\/.)/;

print $1, "\n";

But you would be much better off copying the whole string and removing the slash and everything after it, like this 但是 ,最好复制整个字符串并删除斜杠及其后的所有内容,就像这样

use strict;
use warnings;

my $s = '@ERR030882.2595 HWI-BRUNOP16X_0001:3:1:6649:5175#0/1';

(my $fixed = $s) =~ s{/\d+$}{};

print $fixed, "\n";

output 产量

@ERR030882.2595 HWI-BRUNOP16X_0001:3:1:6649:5175#0

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

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