473,414 Members | 2,019 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,414 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 2713
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.