B
B
Boshmax2014-02-23 15:52:20
PHP
Boshmax, 2014-02-23 15:52:20

How to write a regular expression (equal to "[Tt][Ee][Xx][Tt]" and not equal to "text")?

You need to find occurrences of a word that has uppercase letters, but there were no words written entirely in lowercase.
For example, you need to find the word "TeXt" with different letter cases ([Tt][Ee][Xx][Tt]), but not to find the word "text" because it is in lowercase
Text - true
TeXt - true teXt
- true
TEXT - true
text - false
add
.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Andrey Dugin, 2014-03-21
@Boshmax

Through a negative lookbehind/lookahead assertion:

#!/usr/bin/python2.7
# -*- coding: utf-8 -*-

import re

test = """Text - true
TeXt - true
teXt - true
TEXT - true
text - false"""

reg1 = re.compile(ur"([Tt][Ee][Xx][Tt])(?<!text)")
reg2 = re.compile(ur"(?!text)([Tt][Ee][Xx][Tt])")

print reg1.findall(test)
print reg2.findall(test)

Result:
Z:\>test.py
['Text', 'TeXt', 'teXt', 'TEXT']
['Text', 'TeXt', 'teXt', 'TEXT']

E
egor_nullptr, 2014-02-23
@egor_nullptr

/\s([a-zA-Z]*[AZ]+[a-zA-Z]*)\s/

B
Boshmax, 2014-02-23
@Boshmax

Not exactly what you need.
There is some text, but you need to search not for all words (with large and small letters), but for a specific search word that occurs, but not in lower case.

E
egor_nullptr, 2014-02-23
@egor_nullptr

match /\s(test)\s/i && not match /\stest\s/
php:

if (preg_match_all('/\s(test)\s/i', $text, $matches) && !preg_match('/\stest\s/', $text)) {
    print_r($matches);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question