简体   繁体   中英

Regex to search for only carriage return and newline character in string

I am Newbie to regex and need help to figure out how to form a regex to return False if Section One is empty and has no alphanumeric characters. Currently I am using the following regex, but it still considers newline, carriage return characters as strings.

Any help would be appreciated

PATTERN = r'Section One:[\s]*'

Input:

1. Section One:

2. Section Two: lknlknvd lknlfnv
lvkndflvlkvndflvkn
vlkfdnlkfn

3. Section Three:pklnklnfvl lknlknvl
flvkndflknvlf 

One option is to use the following pattern:

Section One:\s*(?:\d+\.\s+Section|$)

This will be a positive match when the text Section One: is followed by nothing but whitespace before either another section header begins or the end of the string is hit. I left open the possibility that maybe some of your inputs would only have a single empty section one.

import re

line = "1.Section One:\n\nSection Two: lknlknvd lknlfnv\nlvkndflvlkvndflvkn\nvlkfdnlkfn\n\n3. Section Three:pklnklnfvl lknlknvl\nflvkndflknvlf "

obj = re.match( r'.*Section One:\s+(?:\d+\.\s+Section|$)', line, re.M|re.I)

if obj:
    print "Section One has no content"
else:
    print "Section One has content"

Demo

You may want this one Section One:.*[a-z0-9].*$

re.MULTILINE and re.IGNORECASE are used.

.*[a-z0-9].* : Match something contains at least one alphanumeric character, ie. [a-z0-9] .
$ : make sure matching against the whole line.

See https://regex101.com/r/H64ZCV/3 for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM