RegEx: Unterschied zwischen den Versionen

Aus MattWiki
Keine Bearbeitungszusammenfassung
 
(21 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
Notes for RegEx respectively Regular Expressions.
== Introduction ==


Guide (german): https://t3n.de/news/regex-guide-t3n-552858/
This article contains notes for RegEx respectively Regular Expressions and also some useful ressources.
 
RegEx are not Regular Expressions in the narrower sense of the word but today both tearms are used more or less interchangably. More on that: https://www.rexegg.com/regex-vs-regular-expression.html


== General Rules ==
== General Rules ==


{| class="wikitable"
{| class="wikitable"
|+ style="text-align: left" | Character Types
|+ style="text-align: left" | Meta Characters
! Type !! Example
! Meta Character !! Example
|-
| Single Character  || .
|-
| Character class digit  || \d
|-
| Character class character  || \w
|-
|
Character class whitespace <br>
(Space, tab, new line, carriage return)
|| \s
|-
|-
| Literal || a b c 1 2 3
| Inversion of character classes (Not) || \D, \W, \S
|-
|-
| Meta characters || . [[ ] { } ? * + | ( ) ^ $
| Word and text boundaries  || b ^ $
|}
|}


{| class="wikitable"
|+ style="text-align: left" | Ranges
! Range !! Example
|-
| Character class range||  [a-c]
|-
| Inversion of character class range || [^abc]
|}


{| class="wikitable"
{| class="wikitable"
|+ style="text-align: left" | Meaning of meta characters
|+ style="text-align: left" | Quantifiers
! Meaning !! Example
! Quantifier !! Example
|-
| Zero or one character  || ?
|-
|-
| Character class digit  || d
| Zero or more characters (Kleene Star) || *
|-
| One or more characters  (Kleene Plus) || +
|-
| Three characters || {3}
|-
| Four to 20 characters || {4,20}
|}
 
 
{| class="wikitable"
|+ style="text-align: left" | White Space Characters
! White Space !! Example
|-
|-
| Character class character  || w
| Space || _
|-
|-
| Character class whitespace  || s
| Tab || \t
|-
|-
| Inversion of Character Classes  || D, W, S, [^abc]
| New line || \n
|-
|-
| Character class range|| [a-c]
| Carriage return || \r
|-
|-
| Single Character  || .
| Any white space character || \s
|}
 
 
{| class="wikitable"
|+ style="text-align: left" | Line start and end
! Type !! Example
|-
|-
| Grouping with alternatives  || (Mon|Tues)day
| Start of line || ^
|-
|-
| Word and text boundaries  || b ^ $
| End of line || $
|}
|}




{| class="wikitable"
{| class="wikitable"
|+ style="text-align: left" | Multiplier
|+ style="text-align: left" | Groups and nested groups
! Multiplier !! Example
! Type !! String !! Expression !! Results
|-
|-
| Multiplier  || {42}, {4,20}
| Group Example 1 || IMG1000.png || ^(IMG\d+\.png)$ || IMG1000.png
|-
|-
| One or more characters || +
| Group Example 2 || IMG1000.png || ^(IMG\d+)\.png$ || IMG1000
|-
|-
| Zero or one character  || ?
| Nested group || IMG1000.png || ^(IMG(\d+))\.png$ ||
Group 1: IMG1000 <br>
Group 2: 1000
|-
| Multiple groups || 1920x1080 || (\d+)x(\d+) ||
Group 1: IMG1000 <br>
Group 2: 1000
|-
|-
| Zero or more characters  || *
| Grouping with alternatives  || Monday or Tuesday || (Mon&#124;Tues)day ||  
Match 1: Monday <br>
Group 1: Mon <br>
Match 2: Tuesday <br>
Group 2: Tues
|}
|}
== Online Regex Tools ==
* Interpreter
** https://regexr.com/
** https://regex101.com/
* Visualization
** https://regexper.com/
== Tutorials and Guides ==
Guide (german): https://t3n.de/news/regex-guide-t3n-552858/
Quick Start Cheat Sheet: https://www.rexegg.com/regex-quickstart.html
Advanced tutorial: https://www.rexegg.com/regex-disambiguation.html
Practical tutorial: https://regexone.com/
Regex with JavaScript: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/#regular-expressions
[[Category:RegEx]]

Aktuelle Version vom 1. September 2021, 08:14 Uhr

Introduction

This article contains notes for RegEx respectively Regular Expressions and also some useful ressources.

RegEx are not Regular Expressions in the narrower sense of the word but today both tearms are used more or less interchangably. More on that: https://www.rexegg.com/regex-vs-regular-expression.html

General Rules

Meta Characters
Meta Character Example
Single Character .
Character class digit \d
Character class character \w

Character class whitespace
(Space, tab, new line, carriage return)

\s
Inversion of character classes (Not) \D, \W, \S
Word and text boundaries b ^ $
Ranges
Range Example
Character class range [a-c]
Inversion of character class range [^abc]
Quantifiers
Quantifier Example
Zero or one character ?
Zero or more characters (Kleene Star) *
One or more characters (Kleene Plus) +
Three characters {3}
Four to 20 characters {4,20}


White Space Characters
White Space Example
Space _
Tab \t
New line \n
Carriage return \r
Any white space character \s


Line start and end
Type Example
Start of line ^
End of line $


Groups and nested groups
Type String Expression Results
Group Example 1 IMG1000.png ^(IMG\d+\.png)$ IMG1000.png
Group Example 2 IMG1000.png ^(IMG\d+)\.png$ IMG1000
Nested group IMG1000.png ^(IMG(\d+))\.png$

Group 1: IMG1000
Group 2: 1000

Multiple groups 1920x1080 (\d+)x(\d+)

Group 1: IMG1000
Group 2: 1000

Grouping with alternatives Monday or Tuesday (Mon|Tues)day

Match 1: Monday
Group 1: Mon
Match 2: Tuesday
Group 2: Tues

Online Regex Tools

Tutorials and Guides

Guide (german): https://t3n.de/news/regex-guide-t3n-552858/

Quick Start Cheat Sheet: https://www.rexegg.com/regex-quickstart.html

Advanced tutorial: https://www.rexegg.com/regex-disambiguation.html

Practical tutorial: https://regexone.com/

Regex with JavaScript: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/#regular-expressions