Solved

Regular Expression Question (easy)

Posted on 2004-04-13
5
226 Views
Last Modified: 2010-04-16
I am processing a list of filenames and would like to determine which files do not end in ",v" using regular expressions.

Obviously to find which filenames DO end in ",v" I would use ".*,v$".

How do I negate that?
0
Comment
Question by:derekl
5 Comments
 
LVL 2

Accepted Solution

by:
Traltixx earned 25 total points
ID: 10830060
well, you can do an if statement to do:

import re

def findV(word):
     if re.search('.*,v$',word) :
          return "v-ed"
    else
          return "not v-ed"


or something hard like:

re.search('.*,[a-u,w-z],word)
or the longer version
re.search('.*,[abcdefghijklmnopqrstuwxyz],word)

note taking into accoutn that it is not case sensitive...hope this helps



0
 

Assisted Solution

by:DEsterkin
DEsterkin earned 25 total points
ID: 10833235
To get the negation in a single expression:

 for f in files:
   if re.search(".*[^(?:,v)]$", f):
     print f

Hope this helps,

0
 
LVL 2

Expert Comment

by:alain_tesio
ID: 10997259
You don't need a regular expression for this, just test fileName.endswith(",v")
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now