Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Optionale Gruppe in regulärem Ausdruck (in Python)
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum
View previous topic :: View next topic  
Author Message
l3u
Advocate
Advocate


Joined: 26 Jan 2005
Posts: 2538
Location: Konradsreuth (Germany)

PostPosted: Sat Jul 07, 2012 8:54 am    Post subject: Optionale Gruppe in regulärem Ausdruck (in Python) Reply with quote

Hallo :-)

Ich habe ein kleines Problem mit einem regulären Ausdruck und krieg’s nicht gebacken. Ich will einen String in der Form "adresse+befehl@server" bzw. "adresse@server" in seine Bestandteile zerlegen (in Python). Bisher mache ich das „zu Fuß“, indem ich erst nach dem @ splitte, und dann den ersten Teil nach einem +. Wenn’s dabei zwei Treffer gibt, dann gibt es Adresse und Befehl, wenn nicht, dann gibt es nur Adresse.

Jetzt hab ich mir gedacht, dass das ja sicher elegant in einem einzigen regulären Ausdruck geht. Aber wie gesagt: ich bekomm’s nicht hin. Wenn ich nach
Code:
'(.+)\+(.+)@(.+)'

suche, dann bekomme ich, was ich will – aber nur, wenn es ein + im ersten Teil gibt. Sowas wie das hier
Code:
'(.+)\+?(.+)?@(.+)'
'(.+)(\+(.+))?@(.+)'

funktioniert leider auch nicht …

Geht das in einem regulären Ausdruck? Er soll eben für beide Fälle funktionieren und einen leeren String oder ein None für „Befehl“ liefern, wenn es keinen gibt.

Für Hilfe wäre ich sehr dankbar :-)


Last edited by l3u on Sun Jul 08, 2012 10:11 pm; edited 1 time in total
Back to top
View user's profile Send private message
toralf
Developer
Developer


Joined: 01 Feb 2004
Posts: 3919
Location: Hamburg

PostPosted: Sun Jul 08, 2012 8:57 pm    Post subject: Reply with quote

Quote:
Für Hilfe wäre ich sehr dankbar :-)
Dankbar sind bestimmt auch viele Forenteilnehmer, wenn Du das Schlüsselwort "Python" noch mit in den Titel nehmen tätest ... ;-)
Back to top
View user's profile Send private message
l3u
Advocate
Advocate


Joined: 26 Jan 2005
Posts: 2538
Location: Konradsreuth (Germany)

PostPosted: Sun Jul 08, 2012 10:11 pm    Post subject: Reply with quote

Ich bin mir nicht sicher, ob das ein pythonspezifisches Problem ist. Ist eigentlich eher eine generelle Fragestellung. Aber ich schreib’s mal dazu.
Back to top
View user's profile Send private message
Necoro
Veteran
Veteran


Joined: 18 Dec 2005
Posts: 1912
Location: Germany

PostPosted: Mon Jul 09, 2012 7:37 am    Post subject: Reply with quote

Das Problem (was häufig auftritt) ist, dass regexps greedy sind - d.h. die matchen so viel wie möglich.

Du musst daher das '+' für die erste Gruppe ausschließen: ([^+]+)

Code:
>>> r = re.compile(r'([^+]+)\+?(.+)?@(.+)')
>>> r.match("bla+du@blubb.de").groups()
('bla', 'du', 'blubb.de')
>>> r.match("bladu@blubb.de").groups()
('bladu', None, 'blubb.de')


Alternativ: Python erlaubt es, die Qualifier non-greedy zu machen (+? statt +, *? statt *, ?? statt ?). Denn muss man aber darauf achten, dass die zweite Hälfte nur gematcht wird wenn es wirklich ein Plus gibt:

Code:
>>> r = re.compile(r'(.+?)(?:\+(.+))?@(.+)')
>>> r.match("bladu@blubb.de").groups()
('bladu', None, 'blubb.de')
>>> r.match("bla+du@blubb.de").groups()
('bla', 'du', 'blubb.de')


Das (?:...) ist ein "non-grouping" Klammerausdruck -- d.h. es fasst die beiden Sachen für die RE selber zusammen, taucht aber anschließend nicht in groups() auf.
_________________
Inter Deum Et Diabolum Semper Musica Est.
Back to top
View user's profile Send private message
l3u
Advocate
Advocate


Joined: 26 Jan 2005
Posts: 2538
Location: Konradsreuth (Germany)

PostPosted: Mon Jul 09, 2012 6:48 pm    Post subject: Reply with quote

DANKE! Auf das Erste hätt ich selber kommen können. Mit dem Zweiten hab ich auch schon rumexperimentiert, aber ich hab’s nicht hinbekommen.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Deutsches Forum (German) Diskussionsforum All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum