简体   繁体   中英

Perl Regex to match everything after @ character

I have a bunch of text files that contain tags referenced by the @ symbol. For eg a note is tagged 'home' if the note contains @home .

I am trying to find a Perl Regex that will match everything after the @ character but not including the @character.

I have this so far (@\\w+) which successfully matches the whole tag (for .eg it matches @home , @work etc) but I cant find a way to modify it so only the characters after the @ character get picked up.

I had a look at this perl regex to match all words following a character but I couldnt seem to work it out from this.

Any help would be great.

As @Quentin said, @(\\w+) is the best solution.

#!/usr/bin/perl

while (<>) {
    while (/@(\w+)/g) {
        print $1, "\n";
    }
}

If you DO want to match the tag exactly, you can try (?<=@)\\w+ instead. It matches every characters after the @ , but @ excluded.

#!/usr/bin/perl

while (<>) {
    while (/(?<=@)\w+/g) {
        print $&, "\n";
    }
}

Reference: Using Look-ahead and Look-behind

只需将@移动到捕获组之外即可:

@(\w+) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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