473,372 Members | 958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

regex syntax

Hi,

ich kann nicht gut regex, aber für das nötigste reicht es eigentlich.
Irgendwie komm ich aber mit der Syntax der re.* Befehle in Python nicht
klar Vielleicht kann mir das an diesem Beispiel jemand zeigen:

string = "bild.jpg"

ich möchte jetzt einfach wissen ob in dem string ein ".jpg" vorkommt
oder nicht und dann eine Entscheidung treffen. Ich hab mir schon
überlegt einfach die letzten viel Stellen des strings "per Hand" auf die
Zeichenfolge zu vergleichen und so regex zu umgehen. Aber ich muss es
irgendwann ja doch mal nutzen ;-)

Gruß
Andreas
Jul 18 '05 #1
4 2710
Andreas Volz <usenet-spam-t...@brachttal.net> schrieb:
Ich hab mir schon überlegt einfach die letzten viel Stellen des
strings "per Hand" auf die Zeichenfolge zu vergleichen und so
regex zu umgehen. Aber ich muss es irgendwann ja doch mal nutzen


"Muss"? stimme nicht zu! Regexps sind ja fuer begrenzte Zwecke eine
gute Loesung, aber kein Basisteil der Programmierung. Bei diesem
Beispiel waere:
filename.endswith('.jpg')
viel besser als das vergleichbare Regexp:
re.match('.*\.jpg$', filename)

--
Andrew Clover
mailto:an*@doxdesk.com
http://www.doxdesk.com/

Jul 18 '05 #2
Ich kann nicht spricht Deutch, aber:

import re

regex = re.compile(r'(.*)(\.jpg)$')

m = regex.match("bild.jpg")

g = m.groups()

print g[0]
print g[1]
Auf wiedersehen!
James

On Monday 06 December 2004 04:03 pm, Andreas Volz wrote:
Hi,

ich kann nicht gut regex, aber für das nötigste reicht es eigentlich.
Irgendwie komm ich aber mit der Syntax der re.* Befehle in Python nicht
klar Vielleicht kann mir das an diesem Beispiel jemand zeigen:

string = "bild.jpg"

ich möchte jetzt einfach wissen ob in dem string ein ".jpg" vorkommt
oder nicht und dann eine Entscheidung treffen. Ich hab mir schon
überlegt einfach die letzten viel Stellen des strings "per Hand" auf die
Zeichenfolge zu vergleichen und so regex zu umgehen. Aber ich muss es
irgendwann ja doch mal nutzen ;-)

Gruß
Andreas


--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
611 Charles E. Young Dr. S.
MBI 205, UCLA 951570
Los Angeles CA 90095-1570
http://www.jamesstroud.com/
Jul 18 '05 #3
Andreas Volz wrote:
string = "bild.jpg"

ich möchte jetzt einfach wissen ob in dem string ein ".jpg" vorkommt
oder nicht und dann eine Entscheidung treffen. Ich hab mir schon
überlegt einfach die letzten viel Stellen des strings "per Hand" auf die
Zeichenfolge zu vergleichen und so regex zu umgehen. Aber ich muss es
irgendwann ja doch mal nutzen ;-)


1.) If someone else should read your code:

import os

def hasExt (filename, expectedExtension):
actualExtension = os.path.splitext (filename) [1]
return actualExtension == expectedExtension

if hasExt ("build.jpg", ".jpg"):
...

2.) Debugging regular expression:

Just remove meaningful chunks from the end of the expression until you
have an expected match. Then add them again one by one, most problems
will become quite obvious. And how do I wish there was a debug mode that
would make that somewhat automatic.

Daniel
Jul 18 '05 #4
Am 6 Dec 2004 17:43:21 -0800 schrieb an********@doxdesk.com:
viel besser als das vergleichbare Regexp:
re.match('.*\.jpg$', filename)

Ok,now I've choosen this regex:
'.*\.(?i)jpe?g'


to get .jpg .JPG .jpeg .JPEG

seems to work. Is this correct?

regards
Andreas
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: zOrg | last post by:
hi, i'm using the preg_match_all() function to parse an asp file and find all include file within this file : asp include strings can be : <!--#include virtual="/dir/file.asp"--> or...
75
by: Xah Lee | last post by:
http://python.org/doc/2.4.1/lib/module-re.html http://python.org/doc/2.4.1/lib/node114.html --------- QUOTE The module defines several functions, constants, and an exception. Some of the...
8
by: Johnny | last post by:
I need to determine whether a text box contains a value that does not convert to a decimal. If the value does not convert to a decimal, I want to throw a MessageBox to have the user correct the...
3
by: Stephan Bour | last post by:
I have a string ³Name² in the following format: ³LastName, FirstName (Department)² that comes from Active Directory. I need to extract the FirstName from the string. Substrings are not practical...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
5
by: lgbjr | last post by:
Hello All, I have the following type of string: "X:Y\Z.exe" "123" What I need is an array of strings with the information from within each set of quotes. I was trying to use a Regex.Split, but...
3
by: jg | last post by:
I made a mistake somewhere in my vb code and I look, check and read against the articles and help on regex, I still can't find the mistake I made. I know my test string and the test patterns...
3
by: gisleyt | last post by:
I'm trying to compile a perfectly valid regex, but get the error message: r = re.compile(r'(*)(\d{1,3}\.\d{0,2})?(\d*)(\,\d{1,3}\.\d{0,2})?(\d*)?.*') Traceback (most recent call last): File...
8
by: Xah Lee | last post by:
the Python regex documentation is available at: http://xahlee.org/perl-python/python_re-write/lib/module-re.html Note that, i've just made the terms of use clear. Also, can anyone answer what...
2
by: rockstar_ | last post by:
Hello all- I'm developing a Content Management software for my own site, and possibly package and deploy to other sites (for friends, family, etc.) The content management software is combined...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.