Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 379 characters in body
Source Link
ankh-morpork
  • 1.5k
  • 1
  • 12
  • 15

Haskell, 204203 bytes

import Data.List
r&s=elem""$fst(p r)[s]
p(c:t)|c>'`'=t% \s->[t|h:t<-s,c==h]|c>'^'=t%id|(l,o:t)<-p t,(r,_:u)<-p t=u%last(r.l:[\s->r s++l s|o>'+'])
m#s=nub$s++mm#s=s++filter(`notElem`s)(m s)
('*':t)%m=t%until(\s->s==m#s)(m#)
s%m=(m,s)

Try it online! Try it online!

To get rid of one byte, I replaced import Data.List; m#s=nub$s++m s with m#s=s++filter(`notElem`s)(m s). These functions aren't equivalent if there are duplicate elements in either s of m s. The new function does, however, remove all elements from m s that already exist in s, so until still terminates once no new suffixes are discovered by the application of m.

Haskell, 204 bytes

import Data.List
r&s=elem""$fst(p r)[s]
p(c:t)|c>'`'=t% \s->[t|h:t<-s,c==h]|c>'^'=t%id|(l,o:t)<-p t,(r,_:u)<-p t=u%last(r.l:[\s->r s++l s|o>'+'])
m#s=nub$s++m s
('*':t)%m=t%until(\s->s==m#s)(m#)
s%m=(m,s)

Try it online!

Haskell, 203 bytes

r&s=elem""$fst(p r)[s]
p(c:t)|c>'`'=t% \s->[t|h:t<-s,c==h]|c>'^'=t%id|(l,o:t)<-p t,(r,_:u)<-p t=u%last(r.l:[\s->r s++l s|o>'+'])
m#s=s++filter(`notElem`s)(m s)
('*':t)%m=t%until(\s->s==m#s)(m#)
s%m=(m,s)

Try it online!

To get rid of one byte, I replaced import Data.List; m#s=nub$s++m s with m#s=s++filter(`notElem`s)(m s). These functions aren't equivalent if there are duplicate elements in either s of m s. The new function does, however, remove all elements from m s that already exist in s, so until still terminates once no new suffixes are discovered by the application of m.

added 1925 characters in body
Source Link
ankh-morpork
  • 1.5k
  • 1
  • 12
  • 15

GolfScript , 198 bytes

I was able to beat my Haskell solution by implementing the first algorithm I tried in GolfScript instead of Haskell. I don't think it's interesting enough for a separate answer, so I'll just leave it here. There are likely some golfing opportunities since I learned GolfScript just for this.

This solution is in the form of a block that expects the test string on the top of the stack followed by the regex string.

{[.;]1円+{(.96>{0[\]}{2%0{r\(\r\:s;[@]s(;\}i}{if}:i~\{(.3%}{;2円[\]\}until[.;]\+\}:r~\;{.{(.{.4%{2%{)@\m\)\;m}{)\;{.@.@m 1$|.@={\;}{\o}i}:o~}i}{;)@.@m@@\)\;m|}i}{;(:c;;{,},{(\;c=},{(;}%}i}{;}i}:m~""?}

Try it online!


GolfScript , 198 bytes

I was able to beat my Haskell solution by implementing the first algorithm I tried in GolfScript instead of Haskell. I don't think it's interesting enough for a separate answer, so I'll just leave it here. There are likely some golfing opportunities since I learned GolfScript just for this.

This solution is in the form of a block that expects the test string on the top of the stack followed by the regex string.

{[.;]1円+{(.96>{0[\]}{2%0{r\(\r\:s;[@]s(;\}i}{if}:i~\{(.3%}{;2円[\]\}until[.;]\+\}:r~\;{.{(.{.4%{2%{)@\m\)\;m}{)\;{.@.@m 1$|.@={\;}{\o}i}:o~}i}{;)@.@m@@\)\;m|}i}{;(:c;;{,},{(\;c=},{(;}%}i}{;}i}:m~""?}

Try it online!

Bounty Awarded with 100 reputation awarded by Deadcode
deleted 2 characters in body
Source Link
ankh-morpork
  • 1.5k
  • 1
  • 12
  • 15

Haskell, 205204 bytes

Nobody had done this by implementing a small regex engine yet, and I felt like it had to be done. This obviously won't win. but I'm hoping it will inspire someone to write an even more golfed regex engine.

I've rewritten my solution to avoid directly parsing the regular expression into its AST. Instead, the parsing process constructs a function that is used to match a string against the input regex.

The main function is (&) :: String -> String -> Bool which takes a string representation of a regex and a string to test, returning a boolean value. This calls to the next function which handles most of the work in parsing the regex and matching the string.

Function p :: String -> ([String] -> [String], String) takes a string representation of a regex and returns as the first element of a tuple a function that returns a list of all possible unmatched suffixes of strings in the input list after satisfying the regex parsed from the input string. The regex fully matches the string if the empty string is contained in the list of possible unmatched suffixes.

import Data.List
r&s=elem""$fst(p r)[s]
p(c:t)|c>'`'=t% \s->[t|h:t<-s,c==h]|c>'^'=t%id|(l,o:t)<-p t,(r,_:u)<-p t=u%t=u%last(last$rr.l:[\s->r s++l s|o>'+'])
m#s=nub$s++m s
('*':t)%m=t%until(\s->s==m#s)(m#)
s%m=(m,s)

Try it online! Try it online!

Ungolfed Code

import Data.List
match :: String -> String -> Bool
match r s =elem ""$(fst $ parseRegex r)[s]
parseRegex :: String -> ([String] -> [String], String)
parseRegex ('_':t) = parseKleene id t
parseRegex (c:t) | c >= 'a' = parseKleene (>>=p) t
 where p (c':t')| c==c' = [t']
 p _ = []
parseRegex ('(':t) =
 let (l, (o:t')) = parseRegex t in
 let (r, (_:t'')) = parseRegex t' in
 parseKleene (if o=='+' then (r.l) else (\ss-> (r ss)++(l ss))) t''
parseKleene :: ([String] -> [String]) -> String -> ([String] -> [String], String)
parseKleene p ('*':t) = parseKleene p' t
 where
 p' ss
 | ss' <- nub$ p ss,
 ss /= ss' = ss ++ (p' ss')
 | otherwise = ss
parseKleene p s = (p,s)

Haskell, 205 bytes

Nobody had done this by implementing a small regex engine yet, and I felt like it had to be done. This obviously won't win. but I'm hoping it will inspire someone to write an even more golfed regex engine.

I've rewritten my solution to avoid directly parsing the regular expression into its AST. Instead, the parsing process constructs a function that is used to match a string against the input regex.

The main function is (&) :: String -> String -> Bool which takes a string representation of a regex and a string to test, returning a boolean value. This calls to the next function which handles most of the work in parsing the regex and matching the string.

Function p :: String -> ([String] -> [String], String) takes a string representation of a regex and returns as the first element of a tuple a function that returns a list of all possible unmatched suffixes of strings in the input list after satisfying the regex parsed from the input string. The regex fully matches the string if the empty string is contained in the list of possible unmatched suffixes.

import Data.List
r&s=elem""$fst(p r)[s]
p(c:t)|c>'`'=t% \s->[t|h:t<-s,c==h]|c>'^'=t%id|(l,o:t)<-p t,(r,_:u)<-p t=u%(last$r.l:[\s->r s++l s|o>'+'])
m#s=nub$s++m s
('*':t)%m=t%until(\s->s==m#s)(m#)
s%m=(m,s)

Try it online!

Ungolfed Code

import Data.List
match :: String -> String -> Bool
match r s =elem ""$(fst $ parseRegex r)[s]
parseRegex :: String -> ([String] -> [String], String)
parseRegex ('_':t) = parseKleene id t
parseRegex (c:t) | c >= 'a' = parseKleene (>>=p) t
 where p (c':t')| c==c' = [t']
 p _ = []
parseRegex ('(':t) =
 let (l, (o:t')) = parseRegex t in
 let (r, (_:t'')) = parseRegex t' in
 parseKleene (if o=='+' then (r.l) else (\ss-> (r ss)++(l ss))) t''
parseKleene :: ([String] -> [String]) -> String -> ([String] -> [String], String)
parseKleene p ('*':t) = parseKleene p' t
 where
 p' ss
 | ss' <- nub$ p ss,
 ss /= ss' = ss ++ (p' ss')
 | otherwise = ss
parseKleene p s = (p,s)

Haskell, 204 bytes

Nobody had done this by implementing a small regex engine yet, and I felt like it had to be done. This obviously won't win. but I'm hoping it will inspire someone to write an even more golfed regex engine.

I've rewritten my solution to avoid directly parsing the regular expression into its AST. Instead, the parsing process constructs a function that is used to match a string against the input regex.

The main function is (&) :: String -> String -> Bool which takes a string representation of a regex and a string to test, returning a boolean value. This calls to the next function which handles most of the work in parsing the regex and matching the string.

Function p :: String -> ([String] -> [String], String) takes a string representation of a regex and returns as the first element of a tuple a function that returns a list of all possible unmatched suffixes of strings in the input list after satisfying the regex parsed from the input string. The regex fully matches the string if the empty string is contained in the list of possible unmatched suffixes.

import Data.List
r&s=elem""$fst(p r)[s]
p(c:t)|c>'`'=t% \s->[t|h:t<-s,c==h]|c>'^'=t%id|(l,o:t)<-p t,(r,_:u)<-p t=u%last(r.l:[\s->r s++l s|o>'+'])
m#s=nub$s++m s
('*':t)%m=t%until(\s->s==m#s)(m#)
s%m=(m,s)

Try it online!

Ungolfed Code

import Data.List
match :: String -> String -> Bool
match r s =elem ""$(fst $ parseRegex r)[s]
parseRegex :: String -> ([String] -> [String], String)
parseRegex ('_':t) = parseKleene id t
parseRegex (c:t) | c >= 'a' = parseKleene (>>=p) t
 where p (c':t')| c==c' = [t']
 p _ = []
parseRegex ('(':t) =
 let (l, (o:t')) = parseRegex t in
 let (r, (_:t'')) = parseRegex t' in
 parseKleene (if o=='+' then (r.l) else (\ss-> (r ss)++(l ss))) t''
parseKleene :: ([String] -> [String]) -> String -> ([String] -> [String], String)
parseKleene p ('*':t) = parseKleene p' t
 where
 p' ss
 | ss' <- nub$ p ss,
 ss /= ss' = ss ++ (p' ss')
 | otherwise = ss
parseKleene p s = (p,s)
deleted 7 characters in body
Source Link
ankh-morpork
  • 1.5k
  • 1
  • 12
  • 15
Loading
added 33 characters in body
Source Link
ankh-morpork
  • 1.5k
  • 1
  • 12
  • 15
Loading
deleted 45 characters in body
Source Link
ankh-morpork
  • 1.5k
  • 1
  • 12
  • 15
Loading
deleted 37 characters in body
Source Link
ankh-morpork
  • 1.5k
  • 1
  • 12
  • 15
Loading
added 117 characters in body
Source Link
ankh-morpork
  • 1.5k
  • 1
  • 12
  • 15
Loading
deleted 159 characters in body
Source Link
ankh-morpork
  • 1.5k
  • 1
  • 12
  • 15
Loading
Fix nontermination on repeated kleene closure
Source Link
ankh-morpork
  • 1.5k
  • 1
  • 12
  • 15
Loading
edited body
Source Link
ankh-morpork
  • 1.5k
  • 1
  • 12
  • 15
Loading
deleted 11 characters in body
Source Link
ankh-morpork
  • 1.5k
  • 1
  • 12
  • 15
Loading
edited body
Source Link
ankh-morpork
  • 1.5k
  • 1
  • 12
  • 15
Loading
deleted 105 characters in body
Source Link
ankh-morpork
  • 1.5k
  • 1
  • 12
  • 15
Loading
deleted 26 characters in body
Source Link
ankh-morpork
  • 1.5k
  • 1
  • 12
  • 15
Loading
Source Link
ankh-morpork
  • 1.5k
  • 1
  • 12
  • 15
Loading

AltStyle によって変換されたページ (->オリジナル) /