Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
Python

r"
(^ *\S+ *$)|(^ *(\S+ ){1,3}\S+ *$)|(^ *(\S+ ){5,}\S+ *$)|(^ +(\S+ ){4}\S+ +$)
"
mg

Description

In the example i search for all lines which have less or more than 5 spaces as seperator.


modify the valid nr of spaces:

there are 3 (of 4) arbitrary groups wich have an specified nr of copies of an regEx in it: in the example the valid nr of entires is 5 : -> that means 4 spaces are valid in-between the entries

  1. Group with specifed nrs: <REGEX> {1,3} :
  • gets alle invalid Entires with 1 to 3 spaces in-between
  1. Group with specifed nrs: <REGEX> {5,} :
  • gets alle invalid Entires with more than 4 spaces in-between
  1. Group with specifed nrs: <REGEX> {4}:
  • gets alle invalid Entires with have the right nr of spaces in between, but they hava invalid spaces at the start and end of line

The 4. Group matches every entry with has no spaces in between

If you want to change the nr of valid entries to e.g. to 10: --> 9 spaces are valid in-beteween

  1. Group with specifed nrs: {1,9}
  2. Group with specifed nrs: {10}
  3. Group with specifed nrs: {9}

Example for Using:

I´ve used this regEx for checking if names are valid to (own created) name-convention. These convention consists always of 5 entries with non-white-Spaces, and this entries are separated wiht 1 space in between.

Submitted by Mike - 8 years ago