Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[A-z0-9]+ regexp matching square brackets [duplicate]

Tags:

regex

I'm struggling with the following regexp

[A-z0-9]+ 

If tested against this string:

||a919238[.--a]asd| 

it returns a919238[, including the square bracket.. I tried to input my test case on regex101 to understand what's wrong, but the site regex explanation is not helping, probably I'm not able to see my mistake.

Why is the square bracket included in the result?

like image 464
BeNdErR Avatar asked Feb 11 '15 08:02

BeNdErR


People also ask

How do you match square brackets in regex?

You can omit the first backslash. [[\]] will match either bracket. In some regex dialects (e.g. grep) you can omit the backslash before the ] if you place it immediately after the [ (because an empty character class would never be useful): [][] .

What is?= in regex?

(?= regex_here) is a positive lookahead. It is a zero-width assertion, meaning that it matches a location that is followed by the regex contained within (?=

How do you do brackets in regex?

Use Parentheses for Grouping and Capturing. By placing part of a regular expression inside round brackets or parentheses, you can group that part of the regular expression together. This allows you to apply a quantifier to the entire group or to restrict alternation to part of the regex.

What this regex means?

A regular expression (shortened as regex or regexp; sometimes referred to as rational expression) is a sequence of characters that specifies a search pattern in text. Usually such patterns are used by string-searching algorithms for "find" or "find and replace" operations on strings, or for input validation.


1 Answers

Because

[A-z0-9]+   ↑ ↑  

is from A to z, see the ASCII table, ] appears between the two characters:

enter image description here

like image 149
Maroun Avatar answered Sep 28 '22 06:09

Maroun