简体   繁体   中英

Perl Regex either some string is there or not

today i need your help again. Im learning Perl and my first Job is to expand a little script which waits for this Output from a virtual machine this output is:

Info: SOME_RANDOM_STRING: Focus switched to: [b] (1)

OR

Info: Focus switched to: [b] (1)

So the "SOME_RANDOM_STRING:" can be there but doesn't need to now i need to match those to decide if the Output was there or not. I tried

Info: [a-zA-Z0-9*:]* Focus switched to: [b] (1)

And this matches "Info: SOME_RANDOM_STRING: Focus switched to: [b] (1)" but not "Info: Focus switched to: [b] (1)"

You have to capture one of the two spaces enclosing your random string.

This does not match any space at all:

[a-zA-Z0-9:]*

Use this instead:

( [a-zA-Z0-9:]+)?

I'd do:

$str =~ /Info:.*?Focus switched to: \[b\] \(1\)/;

Where .*? matches any number of any character not greedy.

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