I cannot use Boundary( b) to validate a word starting with "@"

Asked

Viewed 147 times

7

In the case of / b@MYVAR b/i, I cannot use Boundary, See: https://regex101.com/r/4bFElA/1 I need to validate a string that contains a @MYVAR word (Example).

Are there any restrictions on this character? What would be an alternative?

I did a javascript test and the result is false.

console.log(`/\b@MYVAR\b/i.test("@MYVAR")`,/\b@MYVAR\b/i.test("@MYVAR"));

In php the code would look something like this:

<?php
$value = "@MYVAR";
if(preg_match("#/\b@MYVAR\b/i#i", trim($value))){
 echo "ok";
}
  • In what language are you going to use this regex? Can you explain better the problem you are trying to solve?

  • I need to validate a string that contains a word @MYVAR, posted an example in javascript but could be in php or any other language.

  • 1

    friend, have you tried using regex (@userid)? @Leonancarvalho

4 answers

5


The Fatherland (\b) applies only to letters, numbers and the underline that would be the equivalent to the \w ([a-zA-Z0-9_]) so your regex fails.

In that case you would have to leave the arroba out of the Boundary. Something like @\buserid\b or @?\buserid\b

  • 1

    Just one detail \w = [a-zA-Z0-9_]

  • @Guilhermelautert thanks for the correction I’ve edited :)

4

The problem is that @ is not considered a valid character for word. That is to say: \b refers to the beginning of a word but the characters valid for \w sane [a-zA-Z0-9_], and @ is not there.

  • 1

    faster than me, damn

  • @Peace :) Next time :)

  • 1

    You move so fast my eyes can barely keep up with you!

4

Complementing the concept:

The \b does not make selection. He is a anchor as well as ^ and $ and position the cursor to the positions that are between: (^\w|\w$|\W\w|\w\W). In other words:

  • At the beginning of the string, given that the first char be a char word.
  • At the end of the string, given that the last char be a char word.
  • In the middle of the string, where a char is a word and the other not.

Is considered a char word whatever is in [a-zA-Z0-9_], i.e., letters, digits and underline.

For example: said "i'm looking @ you", the \b position (I will mark with |):

|i|'|m| |looking| @ |you|

I think these considerations might be useful to some.

2

As quoted by the elders rray and Sergio, a word Boundary is set to start in characters [a-z A-Z 1-9], but this can be reversed with a regex like this:

(@\buserid\b)

Explanation:
This regex identifies the @and only the match character if the word "userid" exists next.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.