Off Topic > Off Topic
Programming Megathread
<< < (49/241) > >>
Foxscotch:
this post might contain some spoilers for day 5 part 2 of the advent of code thing
--- Quote from: SetGaming on December 19, 2015, 09:51:30 AM ---This might be a stupid question, but could somebody explain Regex's to me? I know they're regular expressions for sorting and stuff, but I have no clue how they work.

--- End quote ---
regular expressions aren't for sorting, they're for getting useful information out of text and/or checking whether or not that text matches a pattern

these are the two expressions I had in that code:
.*(\w\w).*\1.*
and
.*(\w)\w\1.*
the first one checks for rule 1:

--- Quote from: Advent of Code ---It contains a pair of any two letters that appears at least twice in the string without overlapping, like xyxy (xy) or aabcdefgaa (aa), but not like aaa (aa, but it overlaps).
--- End quote ---
the second checks for rule 2:

--- Quote from: Advent of Code ---It contains at least one letter which repeats with exactly one letter between them, like xyx, abcdefeghi (efe), or even aaa.
--- End quote ---

I'll break them down. first one's first
.*(\w\w).*\1.*
each dot stands for any character. \w stands for any letter. I could've used \w in place of the dots, but I think it looks marginally clearer this way, and there was no reason that would cause any problems
the asterisks are modifiers for the previous token. specifically, they mean that 0 or more of the previous token should be searched for. + would mean 1 or more, ? would mean 0 or 1, blah blah blah
the parentheses create a group. here, the group contains any two letters. then there's another .*
then there's the \1 which stands for whatever was matched by the first group. \2 would check for the second group, if there was one, and so on

so what it's looking for is any number of letters, followed by a pair of letters that is repeated at any point later in the text
in other words, exactly what rule 1 defines

the second one is pretty similar
.*(\w)\w\1.*
the middle bit is where it differs. the group only contains one letter, and we're still looking for a repeat later. but instead of looking for the repeat after any number of characters, we're specifically looking for it after ONE letter

this is a very good site both for testing regular expressions and learning simple regex syntax
there are different "flavors" of regex, because some people thought more features were necessary. for instance, python supports NAMED groups, which is not on that website. that site just has the features available in javascript's version of regex, although the syntax reference does mention a couple of things JS doesn't have
here's a page that summarizes some of the differences. if the language you're using isn't in that table, it probably inherits from one of the options that is


edit: I'm pretty convinced that nobody else here uses Python as much as I do, and I imagine there's few who use it at all, but I guess I'll post this here anyway, just in case
https://www.reddit.com/r/adventofcode/comments/3xkm1u/
Jairo:


I am become death

clarification: I used up an afternoon to make an image archive in the style of 4chan using html and css
Becquerel:
lolwtf
ZSNO:

--- Quote from: Jairo on December 30, 2015, 05:52:23 PM ---I am become death

--- End quote ---
Wrong thread?
Jairo:

--- Quote from: ZSNO on December 30, 2015, 06:07:36 PM ---Wrong thread?

--- End quote ---
I thought this thread had been revamped to include web development too
Navigation
Message Index
Next page
Previous page

Go to full version