정규식 (.NET 맛) 182 181 145 132 126 114 104 100 98 97 96 바이트
2D ASCII 아트 패턴 인식? 정규식에 대한 직업처럼 들립니다! (그렇지 않습니다.)
나는 이것이 정규식 제출이 유효한 프로그램인지 아닌지에 대해 끊임없는 토론을 다시 시작할 것이라는 것을 알고 있지만, 어쨌든 이것이 APL 또는 CJam을 이길 것이라고 의심하므로 아무런 해가 보이지 않습니다. (밝혔다되는 것을 그들은 어떻게 우리의 다이 하드 시험에 합격 "어떻게 프로그래밍 언어를?" .)
일치하는 문자열로 입력을 받고 결과는 찾은 일치 수입니다. 후자를 탈출해야하기 때문에 _
대신 대신 사용합니다 .
. 또한 후행 줄 바꿈이 필요합니다.
(X(X){1,21})(?=\D+((?>(?<-2>_)+)_))(?=.((?!\7)(.)*
.*(X\3X|()\1.)(?=(?<-5>.)*(?(5)!)
)){4,23}\7)
RegexHero 또는 RegexStorm 에서 실시간으로 테스트 할 수 있습니다 ). 경기는 포털의 최고 흑요석 행이됩니다. 실패한 테스트 사례를 찾으면 알려주십시오!
이 마법은 무엇입니까?
다음 설명은 .NET의 밸런싱 그룹에 대한 기본적인 이해를 가정합니다 . 요점은 캡처가 .NET 정규 표현식의 스택이라는 것입니다. 같은 이름의 모든 새로운 캡처는 스택으로 푸시되지만 해당 스택에서 다시 캡처를 캡처하는 구문과 하나의 스택에서 팝업을 캡처하는 구문 및 푸시 캡처도 있습니다 동시에 다른 사람에게. 더 완전한 그림을 보려면 모든 세부 사항을 다루는 스택 오버플로에 대한 내 대답을 볼 수 있습니다 .
기본 아이디어는 다음과 같은 패턴을 일치시키는 것입니다.
X{n}..{m}
X_{n}X.{m} |
X_{n}X.{m} | 3 to 22 times
X_{n}X.{m} |
X{n}..{m}
어디는 n
2 사이와 22 (포함)입니다. 까다로운 것은 모든 n
s와 모든 m
s를 동일하게 만드는 것입니다 . 실제 문자는 같지 않으므로 역 참조를 사용할 수 없습니다.
정규식에는 줄 바꿈이 포함 \n
되어 있어야하며 다음과 같이 작성합니다.
( # Open capturing group 1. This will contain the top of a portal, which
# I can reuse later to match the bottom (being of the same length).
X # Match a single X.
(X){1,21} # Match 1 to 21 X's, and push each separately on the <2> stack. Let's
# Call the number of X's captured N-1 (so N is the inner width of the
# portal).
) # End of group 1. This now contains N X's.
(?= # Start a lookahead. The purpose of this lookahead is to capture a
# string of N underscores in group 2, so I can easily use this to match
# the inside rows of the portal later on. I can be sure that such a
# string can always be found for a valid portal (since it cannot have 0
# inner height).
\D+ # Skip past a bunch of non-digits - i.e. *any* of the vaild characters
# of the input (_, X, \n). This to make sure I search for my N
# underscores anywhere in the remainder of the input.
( # Open capturing group 3. This will contain a portal row.
(?> # This is an atomic group. Once the engine hass successfully matched the
# contents of this group, it will not go back into the group and try to
# backtrack other possible matches for the subpattern.
(?<-2>_)+ # Match underscores while popping from the <2> stack. This will match as
# many underscores as possible (but not more than N-1).
) # End of the atomic group. There are two possible reasons for the
# subpattern stopping to match: either the <2> stack is empty, and we've
# matched N-1 underscores; or we've run out of underscores, in which
# case we don't know how many underscores we matched (which is not
# good).
_ # We simply try to match one more underscore. This ensures that we
# stopped because the <2> stack was empty and that group 3 will contain
# exactly N underscores.
) # End of group 3.
) # End of the lookahead. We've got what we want in group 2 now, but the
# regex engine's "cursor" is still at the end of the portal's top.
(?= # Start another lookahead. This ensures that there's actually a valid
# portal beneath the top. In theory, this doesn't need to be a
# lookahead - I could just match the entire portal (including the lines
# it covers). But matches cannot overlap, so if there were multiple
# portals next to each other, this wouldn't return all of them. By
# putting the remainder of the check in a lookahead the actual matches
# won't overlap (because the top cannot be shared by two portals).
. # Match either _ or X. This is the character above the portal side.
( # This group (4) is where the real magic happens. It's purpose is to to
# count the length of the rest of the current line. Then find a portal
# row in the next line, and ensure that it's the same distance from the
# end of the line. Rinse and repeat. The tricky thing is that this is a
# single loop which matches both inner portal rows, as well as the
# bottom, while making sure that the bottom pattern comes last.
(?!\7) # We didn't have a group 7 yet... group 7 is further down the pattern.
# It will capture an empty string once the bottom row has been matched.
# While the bottom row has not been matched, and nothing has been
# captured, the backreference will fail, so the negative lookahead will
# pass. But once we have found the bottom row, the backreference will
# always match (since it's just an empty string) and so the lookahead
# will fail. This means, we cannot repeat group 4 any more after the
# bottom has been matched.
(.)* # Match all characters until the end of the line, and push each onto
# stack <5>.
\n # Match a newline to go to the next line.
.* # Match as many characters as necessary to search for the next portal
# row. This conditions afterwards will ensure that this backtracks to
# the right position (if one exists).
( # This group (6) will match either an inner portal row, or the bottom
# of the portal.
X\3X # Match X, then N underscores, then X - a valid inner portal row.
| # OR
() # Capture an empty string into group 7 to prevent matching further rows.
\1. # Use the captured top to match the bottom and another character.
)
(?= # This lookahead makes sure that the row was found at the same
# horizontal position as the top, by checking that the remaining line
# is the same length.
(?<-5>.)* # Match characters while popping from the <5> stack.
(?(5)!)\n # Make sure we've hit end of the line, *and* the <5> stack is empty.
)
){4,23} # Repeat this 4 to 23 times, to ensure an admissible portal height.
# Note that this is one more than the allowed inner height, to account
# for the bottom row.
\7 # Now in the above repetition there is nothing requiring that we have
# actually matched any bottom row - it just ensured we didn't continue
# if we had found one. This backreference takes care of that. If no
# bottom row was found, nothing was captured into group 7 and this
# backreference fails. Otherwise, this backreference contains an empty
# string which always matches.
)
C #, 185 바이트
다음은 완전한 C # 함수입니다. 유효한 항목입니다. .NET 정규식에 대한 명령 줄 "인터프리터"를 작성해야합니다.
static int f(string p){return System.Text.RegularExpressions.Regex.Matches(p,@"(X(X){1,21})(?=\D+((?>(?<-2>_)+)_))(?=.((?!\7)(.)*
.*(X\3X|()\1.)(?=(?<-5>.)*(?(5)!)
)){4,23}\7)").Count;}