string - perl match whole word only with array list -


I should be able to match a user in the list of users, but only one exact match In the sample code given below:
- If there is "information" in Strucer then there is a match and it is good.
- If there is "theinfo" in Strucer, or "Infstrift" is a match and it is not good.
I can not use the \ b modifier because the variable does not have only usernames, spaces, or word boundaries.

  #! / Usr / bin / perl $ strUser = "theinfo"; $ StrUsers = "(alpha | info | omega)"; If ($ strUser = ~ / $ strUsers /) {print "match \ n"; } Exit (0); Use borders within your regex  ^  and    

\ Z :

  If ($ strUser = ~ / ^ $ strUsers \ z /) {  

Note, because you may be able to read literal strings Want to work with, I quotemeta :

  #! Using / usr / bin / perl, I will do more to build my regex; Use warnings; My $ strUser = "theinfo"; My @ user = qw (alpha info omega); My $ list_users_re = join '|', map {quotemeta} @user; If ($ strUser = ~ / ^ ($ list_users_re) \ z /) {print "match \ n"; } Exit (0);  

However, since the search for exact matches, one easy solution is to use only one hash:

  my @users = qw (alpha Information omega); My% is_user = map {$ _ = & gt; 1} @ User; If ($ is_user {$ strUser}) {print "match \ n"; }  

Finally, always include and in every Perl script.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -