Regex to match a word boundary starting with special characters

I have a regular expression that matches words perfectly, except that they contain a special character, for example ~ Request, which is the name of a member of the C ++ class. You must use the word boundary, as shown below, for member names that are single characters. $key =~ /\b$match\b/

I tried a lot of expressions that I thought would work, for example /[~]*\b$match\b/ or /\b[~]*$match\b/

Is it possible to put a word boundary on words that may contain a special character?

+6
source share
2 answers
 \b 

not suitable for

 (?:(?<!\w)(?=\w)|(?<=\w)(?!\w)) 

If you want to treat ~ as a word character, change \w to [\w~] .

 (?:(?<![\w~])(?=[\w~])|(?<=[\w~])(?![\w~])) 

Usage example:

 my $word_char = qr/[\w~]/; my $boundary = qr/(?<!$word_char)(?=$word_char) |(?<=$word_char)(?!$word_char)/x; $key =~ /$boundary$match$boundary/ 

If we know that $match can only $match what starts and ends with $word_char , we can simplify the following:

 my $word_char = qr/[\w~]/; my $start_bound = qr/(?<!$word_char)/; my $end_bound = qr/(?!$word_char)/; $key =~ /$start_bound$match$end_bound/ 

It is simple enough that we can embed.

 $key =~ /(?<![\w~])$match(?![\w~])/ 
+10
source

Assuming you don't need to check the contents of $match (i.e. it always contains a valid identifier), you can write this

 $key =~ /(?<![~\w])$match(?![~\w])/ 

which just checks that the string in $match not preceded or followed by alphanumeric characters, underscores or tildes

+4
source

Source: https://habr.com/ru/post/926862/


All Articles