I am trying to search for a word out of these 2 Principles.
But by code displays both of the principles not just one.

How could I make it display only the principle where the word is located.

import re

patterns = [ 'work' ]
text = """\


1.	Principle of Segmentation:
   	a) Divide an object into independent parts
                 - Replace a large truck by a truck and trailer.
                 - Use a work breakdown structure for a large project.
                 - Sequential novel turn into movies
                 
	b) Make an object easy to disassemble.
		 - Bicycle disassembling (saddle, wheels)
		 - Furniture (sofas, table)
		 - Crib
        c) Increase the degree of fragmentation or segmentation.
                 - Replace solid shades with Venetian blinds
                 - Computer covers with holes for ventilation
		 - Multi blade cartridge Razor


4.	Principle of Asymmetry:
        a.	If an object is symmetrical, change its shape to irregular.
                -Asymmetric paddles mix 
                -cement 
                -truck 
                -blender
                -cake mixer

"""

for pattern in patterns:
    print 'Looking for "%s" in "%s" ->' % (pattern, text),

    if re.search(pattern,  text):
        print 'found a match!'
    else:
        print 'no match'

Recommended Answers

All 12 Replies

You must first find a way to split the text into a list of paragraphs, each containing a principle.

You must first find a way to split the text into a list of paragraphs, each containing a principle.

Could use an example if you have one.
Code is easier to understand.

For example, using re.split()

import re

pat = re.compile(r"\n(?=\d)") # matches newline followed by a digit

text = """\


1.	Principle of Segmentation:
   	a) Divide an object into independent parts
                 - Replace a large truck by a truck and trailer.
                 - Use a work breakdown structure for a large project.
                 - Sequential novel turn into movies
                 
	b) Make an object easy to disassemble.
		 - Bicycle disassembling (saddle, wheels)
		 - Furniture (sofas, table)
		 - Crib
        c) Increase the degree of fragmentation or segmentation.
                 - Replace solid shades with Venetian blinds
                 - Computer covers with holes for ventilation
		 - Multi blade cartridge Razor


4.	Principle of Asymmetry:
        a.	If an object is symmetrical, change its shape to irregular.
                -Asymmetric paddles mix 
                -cement 
                -truck 
                -blender
                -cake mixer

"""
paragraphs = pat.split(text)

for p in paragraphs:
    print "******* PARAGRAPH *********"
    print p
    
print "THERE ARE %d paragraphs" % len(paragraphs)

My output:

******* PARAGRAPH *********


******* PARAGRAPH *********
1.	Principle of Segmentation:
   	a) Divide an object into independent parts
                 - Replace a large truck by a truck and trailer.
                 - Use a work breakdown structure for a large project.
                 - Sequential novel turn into movies
                 
	b) Make an object easy to disassemble.
		 - Bicycle disassembling (saddle, wheels)
		 - Furniture (sofas, table)
		 - Crib
        c) Increase the degree of fragmentation or segmentation.
                 - Replace solid shades with Venetian blinds
                 - Computer covers with holes for ventilation
		 - Multi blade cartridge Razor


******* PARAGRAPH *********
4.	Principle of Asymmetry:
        a.	If an object is symmetrical, change its shape to irregular.
                -Asymmetric paddles mix 
                -cement 
                -truck 
                -blender
                -cake mixer


THERE ARE 3 paragraphs

Thanks, so now to match the word to the cpresponding paragraph i should make a "if true" statemant or something along those lines?

Thanks, so now to match the word to the cpresponding paragraph i should make a "if true" statemant or something along those lines?

One way is to search the pattern in each of the paragraphs in the list instead of the whole text.

One way is to search the pattern in each of the paragraphs in the list instead of the whole text.

Do you mean something like this?

data = [['a','b'], ['a','c'], ['b','d']]
search = 'c'
for sublist in data:
    if sublist[1] == search:
       print "Found it!", sublist
       break

Do you mean something like this?

data = [['a','b'], ['a','c'], ['b','d']]
search = 'c'
for sublist in data:
    if sublist[1] == search:
       print "Found it!", sublist
       break

Rather something like

import re
data = ["One way is to search the pattern", "in each of the paragraphs in the list", "instead of the whole text."] 

for subtext in data:
    if re.search(r"\blist\b", subtext, re.I):
        print "FOUND", subtext

Rather something like

import re
data = ["One way is to search the pattern", "in each of the paragraphs in the list", "instead of the whole text."] 

for subtext in data:
    if re.search(r"\blist\b", subtext, re.I):
        print "FOUND", subtext

Sorry i dont understand what you are trying to tell me there.
Am i ment to but the Principles in data between the " ",
Because i have tried, with no avail.

Rather something like

import re
data = ["One way is to search the pattern", "in each of the paragraphs in the list", "instead of the whole text."] 

for subtext in data:
    if re.search(r"\blist\b", subtext, re.I):
        print "FOUND", subtext

Sorry to bother you agin but i got another question.
Lets ay i wanted to assign a bunch of words to each princile, that if searched for would triggr the principle but NOT display the word/s.

Re: I just got your code :).

Not sure which piece of my code you refered. I have written a few :icon_cheesygrin:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.