Jump to content


Photo

learning python, join me if you'd like


  • Please log in to reply
29 replies to this topic

#21 postSingularityHumanoid

postSingularityHumanoid

    Anti-Brag Queen

  • Patron
  • PipPipPipPipPipPip
  • 64358 posts
  • LocationLimbo

Posted 25 January 2023 - 04:16 AM

Square brackets seems like an "or" and not an "and". Like if I put in [ab] I would get stings that contain a, b or a ava b.

#22 Stains_not_here_man

Stains_not_here_man

    Phat O'Mic Chef Winner!

  • Patron
  • PipPipPipPipPipPip
  • 105019 posts

Posted 25 January 2023 - 08:40 AM

Ok so you want to match any word with an "a" and a "b" in any order?

Match:
banana
absinthe
cabana
baccarat

No match:
ankle
browser
elbow
shanty

?
Also match:

Brand
Drawback

#23 postSingularityHumanoid

postSingularityHumanoid

    Anti-Brag Queen

  • Patron
  • PipPipPipPipPipPip
  • 64358 posts
  • LocationLimbo

Posted 25 January 2023 - 08:44 AM

I think you get what I'm going for, yes.

 

I have the words in a list object like this:

 

word_list = ['apple', 'guy', 'stuck', 'blah']

 

eta:  I picked those words at random, only blah meets the criteria :P


Edited by postSingularityHumanoid, 25 January 2023 - 08:47 AM.


#24 Stains_not_here_man

Stains_not_here_man

    Phat O'Mic Chef Winner!

  • Patron
  • PipPipPipPipPipPip
  • 105019 posts

Posted 25 January 2023 - 08:49 AM

So for that particular example, again this isn't python specific but the regex itself would be

/.*a.*b.*|.*b.*a.*/

.* matches 0 or more of any character (basically a wildcard)

So this translates as

Anything followed by an a, followed by anything, followed by a b, followed by anything. OR anything followed by b, followed by anything, followed by a, followed by anything.

And note that in this context "anything" really means "anything or nothing".

Regexpal.com is your friend btw

Edited by Stains_not_here_man, 25 January 2023 - 08:48 AM.


#25 postSingularityHumanoid

postSingularityHumanoid

    Anti-Brag Queen

  • Patron
  • PipPipPipPipPipPip
  • 64358 posts
  • LocationLimbo

Posted 25 January 2023 - 08:54 AM

So for that particular example, again this isn't python specific but the regex itself would be

/.*a.*b.*|.*b.*a.*/

.* matches 0 or more of any character (basically a wildcard)

So this translates as

Anything followed by an a, followed by anything, followed by a b, followed by anything. OR anything followed by b, followed by anything, followed by a, followed by anything.

And note that in this context "anything" really means "anything or nothing".

Regexpal.com is your friend btw

 

oh, I didn't know these were sort of universal.  so the * always modifies the thing that precedes it, right?  and it basically means, "any number of that thing" and since period is "anything" that's how you arrived there?



#26 Stains_not_here_man

Stains_not_here_man

    Phat O'Mic Chef Winner!

  • Patron
  • PipPipPipPipPipPip
  • 105019 posts

Posted 25 January 2023 - 09:31 AM

Yep!

#27 postSingularityHumanoid

postSingularityHumanoid

    Anti-Brag Queen

  • Patron
  • PipPipPipPipPipPip
  • 64358 posts
  • LocationLimbo

Posted 25 January 2023 - 12:40 PM

Yep!

 

okay, cool.  thanks!



#28 postSingularityHumanoid

postSingularityHumanoid

    Anti-Brag Queen

  • Patron
  • PipPipPipPipPipPip
  • 64358 posts
  • LocationLimbo

Posted 26 January 2023 - 06:11 AM

damn, so something that complicates this a little is the number of letters is variable.  could be 1, 2, 3, etc.

 

you kind of hard coded it for 2 letters but I guess for 3 I'd need 6 small REs divided up with "or"s?  I haven't thought about it too long yet though so I may not have right....



#29 Stains_not_here_man

Stains_not_here_man

    Phat O'Mic Chef Winner!

  • Patron
  • PipPipPipPipPipPip
  • 105019 posts

Posted 26 January 2023 - 06:13 AM

Keep in mind that regex itself is mostly useful for things that are "patterns". In some cases a for loop or a series of them may be more appropriate depending on the variability of the thing you're looking at.

#30 postSingularityHumanoid

postSingularityHumanoid

    Anti-Brag Queen

  • Patron
  • PipPipPipPipPipPip
  • 64358 posts
  • LocationLimbo

Posted 26 January 2023 - 06:19 AM

Keep in mind that regex itself is mostly useful for things that are "patterns". In some cases a for loop or a series of them may be more appropriate depending on the variability of the thing you're looking at.

 

yeah, I was just trying to exercise my RE skills.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users