python regex not detecting square brackets

  • Last Update :
  • Techknowledgy :

You have a lowercase z where it should be upppercase. Change:

re.sub(r "[^a-zA-z0-9 ]+", "", content)

to:

re.sub(r "[^a-zA-Z0-9 ]+", "", content)

Suggestion : 2

Last Updated : 21 Feb, 2022,GATE CS 2021 Syllabus

Output:

Welcome to geeks
for geeks GFG A computer science portal

Suggestion : 3

Square brackets specify a set of characters you wish to match.,A regular expression in a programming language is a special text string used for describing a search pattern. It is extremely useful for extracting information from text such as code, files, log, spreadsheets or even documents.,If you are unsure if a character has special meaning or not, you can put \ in front of it. This makes sure the character is not treated in a special way.,You can also specify a range of characters using - inside square brackets.

A period matches any single character (except newline '\n').

Expression String Matched ?
   a...n abn No match
alian Match
abysn Match
Alian No match
An abacus No match

Expression String Matched?

^ a a Match
abc Match
bac No match
   ^
   ab abc match
acb No match(starts with a, not followed by b)

Suggestion : 4

Several characters or character sets inside square brackets [] mean matching for any character or character set among them.,A set or a range matches any single character or character set specified in square brackets […].,Similarly, you can use one or more character sets inside the square brackets like [\d\s] means a digit or a space character.,To negate a set or a range, you use the caret character (^) at the beginning of the set and range. For example, the range [^0-9] matches any character except a digit. It is the same as the character set \D.

For example, the following program uses the pattern licen[cs]e that matches both license and licence:

.wp - block - code {
      border: 0;
      padding: 0;
   }

   .wp - block - code > div {
      overflow: auto;
   }

   .shcb - language {
      border: 0;
      clip: rect(1 px, 1 px, 1 px, 1 px); -
      webkit - clip - path: inset(50 % );
      clip - path: inset(50 % );
      height: 1 px;
      margin: -1 px;
      overflow: hidden;
      padding: 0;
      position: absolute;
      width: 1 px;
      word - wrap: normal;
      word - break: normal;
   }

   .hljs {
      box - sizing: border - box;
   }

   .hljs.shcb - code - table {
      display: table;
      width: 100 % ;
   }

   .hljs.shcb - code - table > .shcb - loc {
      color: inherit;
      display: table - row;
      width: 100 % ;
   }

   .hljs.shcb - code - table.shcb - loc > span {
      display: table - cell;
   }

   .wp - block - code code.hljs: not(.shcb - wrap - lines) {
      white - space: pre;
   }

   .wp - block - code code.hljs.shcb - wrap - lines {
      white - space: pre - wrap;
   }

   .hljs.shcb - line - numbers {
      border - spacing: 0;
      counter - reset: line;
   }

   .hljs.shcb - line - numbers > .shcb - loc {
      counter - increment: line;
   }

   .hljs.shcb - line - numbers.shcb - loc > span {
      padding - left: 0.75 em;
   }

   .hljs.shcb - line - numbers.shcb - loc::before {
      border - right: 1 px solid #ddd;
      content: counter(line);
      display: table - cell;
      padding: 0 0.75 em;
      text - align: right; -
      webkit - user - select: none; -
      moz - user - select: none; -
      ms - user - select: none;
      user - select: none;
      white - space: nowrap;
      width: 1 % ;
   }
import re

s = 'A licence or license'

pattern = 'licen[cs]e'
matches = re.finditer(pattern, s)

for match in matches:
   print(match.group()) Code language: PHP(php)

Output:

licence
license

The following example uses the caret (^) to negate the set [aeoiu] to match the consonants in the string 'Python':

import re

s = 'Python'

pattern = '[^aeoiu]'
matches = re.finditer(pattern, s)

for match in matches:
   print(match.group())
Code language: JavaScript(javascript)

Suggestion : 5

Alternation; matches any of the patterns p1, p2, or p3 ,Any character not listed between the square brackets,Any character listed between the square brackets,To write a character class, list the characters you want the class to match inside square brackets. Thus, the pattern [abc] matches either a, b, or c.

mysql > SELECT name FROM metal WHERE name REGEXP '^co'; +
-- -- -- -- +
|
name |
   + -- -- -- -- +
   |
   copper |
   + -- -- -- -- +
mysql > SELECT name FROM metal WHERE name REGEXP 'er$'; +
-- -- -- -- +
|
name |
   + -- -- -- -- +
   |
   copper |
   |
   silver |
   + -- -- -- -- +
mysql > SELECT name FROM metal WHERE name REGEXP 'er'; +
-- -- -- -- - +
|
name |
   + -- -- -- -- - +
   |
   copper |
   |
   mercury |
   |
   silver |
   + -- -- -- -- - +
mysql > SELECT name FROM metal WHERE name REGEXP '^..pp'; +
-- -- -- -- +
|
name |
   + -- -- -- -- +
   |
   copper |
   + -- -- -- -- +
mysql > SELECT name FROM metal WHERE name REGEXP '^co'; +
-- -- -- -- +
|
name |
   + -- -- -- -- +
   |
   copper |
   + -- -- -- -- +