Python Forum
a quick check for a pattern?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a quick check for a pattern?
#1
a string might fit the pattern of a window geometry like "700x400" or "700x400+100+100". what is a nice quick way to test if it does and get the numbers from it? the pattern is not valid if what is between the 'x' and '+' characters is not a string representing a number.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
You can use re to do the pattern search. Here is a example:
import re
pattern = re.compile(r"^(\d+)x(\d+)(?:\+(\d+))?(?:\+(\d+))?$")
test = "700x400+400+200"
m = pattern.match(test)
print(m.groups())
// ('700', '400', '400', '200')
Reply
#3
i could not have come up with that. i expected someone would suggest "re". thanks for including the regex.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
what would be the pattern for just "700x400" by itself? i don't understand those question marks so i don't know if i need them. a string lacking digits before or after the "x" need to not match.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A quick question teczone 4 3,079 Sep-06-2018, 03:44 PM
Last Post: snippsat
  Quick help! Ellisrb 2 2,758 May-02-2018, 11:21 AM
Last Post: ThiefOfTime
  quick way to convert in both 2 and 3 Skaperen 10 8,756 Nov-03-2016, 04:43 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020