简体   繁体   中英

Regex , Find the sentence, all of which are capital letters

I need your help.

Currently I'm using this code section for my work;

    altbaslik = []
    for line in sentenceIndex:
        finded = re.match(r"\w*[A-Z]\w*[A-Z]\w*|[Ö|Ç|Ş|Ü|Ğ|İ]", line)
        if finded != None:
          finded2 = finded.group()
          altbaslik.append(finded2)


    print(altbaslik)

sentenceIndex = this is a list. It contains tokenized sentences from a paragraph. For example:


Sample Paragraph:

VODOFONE ARENA ŞANSI Ama asıl önemli olan nokta Murat Çetinkaya, Cumhurbaşkanı Erdoğan ve Başbakan Davutoğlu'nun ittifakıyla seçildi. O süreci ayrıntılı olarak aktaracağım. Hatta Cumhurbaşkanı ve Başbakan'ı aynı isim üzerinde ittifak etmeye götüren kriterlere de değineceğim. Ama bir şey var ki aktarmasam olmaz. Merkez Bankası Başkanı'nın kaderi Dolmabahçe ile Vodafone Arena arasındaki yolculukta belirleniyor.



sentenceIndex:

['VODOFONE ARENA ŞANSI Ama asıl önemli olan nokta Murat Çetinkaya, Cumhurbaşkanı Erdoğan ve Başbakan Davutoğlu'nun ittifakıyla seçildi.','...................','.................']


I need a regex, which finds all the capital letter words in the sentences.

"VODOFONE ARENA ŞANSI" ı need to find and extract this section. current regex that I am using is not working. I need help on this regex thing.

NOTE: [Ö|Ç|Ş|Ü|Ğ|İ] I am working on turkish text. Thats why I need to pay attention this letters too.

Thanks for the people who will spare their time and helped me on this issue :)

To avoid having to list all variants of capitalized characters, install and use the new regex module. It is extremely similar to (as yet) default re but has superior Unicode properties support .

For example, to find any capitalized character you can use the Unicode property \\p{Lu} :

import regex

text = 'VODOFONE ARENA ŞANSI Ama asıl önemli olan nokta Murat Çetinkaya, ' \
       'ΘΑΥΜΑΣΙΟΣ Cumhurbaşkanı Erdoğan ve Başbakan Davutoğlu’nun ittifakıyla seçildi.'

found = regex.findall(r'\b\p{Lu}+(?: \p{Lu}+)*\b', text)
print (found)  # => ['VODOFONE ARENA ŞANSI', 'ΘΑΥΜΑΣΙΟΣ']

You may use re.findall with

r'\b[A-ZÖÇŞÜĞİ]+(?:\W+[A-ZÖÇŞÜĞİ]+)*\b'

With Python regex library that you may install using pip install regex :

r'\b\p{Lu}+(?:\W+\p{Lu}+)*\b'

See the regex demo .

Details

  • \\b - a word boundary
  • [A-ZÖÇŞÜĞİ]+ - 1+ uppercase letters (Base Latin and Turkish) ( \\p{Lu} matches any Unicode uppercase letters)
  • (?:\\W+[A-ZÖÇŞÜĞİ]+)* - 0 or more repetitions of
    • \\W+ - any 1+ non-word chars
    • [A-ZÖÇŞÜĞİ]+ - 1+ uppercase letters (Base Latin and Turkish) ( \\p{Lu} matches any Unicode uppercase letters)
  • \\b - a word boundary

See the Python demo :

import re

altbaslik=[]
sentenceIndex = ['VODOFONE ARENA ŞANSI Ama asıl önemli olan nokta Murat Çetinkaya, Cumhurbaşkanı Erdoğan ve Başbakan Davutoğlu’nun ittifakıyla seçildi.','...................','.................']
for line in sentenceIndex:
    found = re.findall(r"\b[A-ZÖÇŞÜĞİ]+(?:\W+[A-ZÖÇŞÜĞİ]+)*\b", line)
    if len(found):
        altbaslik.extend(found)

print(altbaslik) # => ['VODOFONE ARENA ŞANSI']

Or, with PyPi regex :

import regex

altbaslik=[]
sentenceIndex = ['VODOFONE ARENA ŞANSI Ama asıl önemli olan nokta Murat Çetinkaya, Cumhurbaşkanı Erdoğan ve Başbakan Davutoğlu’nun ittifakıyla seçildi.','...................','.................']
for line in sentenceIndex:
    found = regex.findall(r'\b\p{Lu}+(?:\W+\p{Lu}+)*\b', line)
    if len(found):
        altbaslik.extend(found)

print(altbaslik) # => ['VODOFONE ARENA ŞANSI']

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