Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 776139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:24:17+00:00 2026-05-14T19:24:17+00:00

So, I am writing code to parse and IP Address expression and turn it

  • 0

So, I am writing code to parse and IP Address expression and turn it into a regular expression that could be run against and IP Address string and return a boolean response. I wrote the code in C# (OO) and it was 110 lines of code. I am trying to compare the amount of code and the expressiveness of C# to F# (I am a C# programmer and a noob at F#). I don’t want to post both the C# and F#, just because I don’t want to clutter the post. If needed, I will do so.

Anyway, I will give an example. Here is an expression:

192.168.0.250,244-248,108,51,7;127.0.0.1

I would like to take that and turn it into this regular expression:

((192\.168\.0\.(250|244|245|246|247|248|108|51|7))|(127\.0\.0\.1))

Here are some steps I am following:

Operations:

Break by “;” 192.168.0.250,244-248,108,51,7 127.0.0.1

Break by “.” 192 168 0 250,244-248,108,51,7

Break by “,” 250 244-248 108 51 7
Break by “-” 244 248

I came up with F# that produces the output. I am trying to forward-pipe through my operations listed above, as I think that would be more expressive. Can anyone make this code better? Teach me something 🙂

open System

let createItemArray (group:bool) (y:char) (items:string[]) = 
  [|
    let indexes = items.Length - 1
    let group = indexes > 0 && group
    if group then
      yield "("
    for i in 0 .. indexes do
      yield items.[i].ToString()
      if i < indexes then
        yield y.ToString()
    if group then
      yield ")"
  |] 

let breakBy (group:bool) (x:string) (y:char): string[] = 
  x.Split(y)
    |> createItemArray group y 

let breakItem  (x:string) (y:char): string[] = breakBy false x y
let breakGroup  (x:string) (y:char): string[] = breakBy true x y

let AddressExpression address:string = 
    let builder = new System.Text.StringBuilder "("
    breakGroup address ';'
    |> Array.collect (fun octet -> breakItem octet '.')
    |> Array.collect (fun options -> breakGroup options ',')
    |> Array.collect (fun (ranges : string) -> 
                            match (breakGroup ranges '-') with
                            | x when x.Length > 3
                              -> match (Int32.TryParse(x.[1]), Int32.TryParse(x.[3]))    with
                                  | ((true, a) ,(true, b))
                                      -> [|a .. b|]
                                          |> Array.map (int >> string)
                                          |> createItemArray false '-'
                                  | _ -> [|ranges|]
                            | _ -> [|ranges|]
                    )
    |> Array.iter (fun item ->
                    match item with
                    | ";" -> builder.Append ")|("
                    | "." -> builder.Append "\."
                    | "," | "-" -> builder.Append "|"
                    | _ -> builder.Append item
                    |> ignore
                  )
    builder.Append(")").ToString()

let address = "192.168.0.250,244-248,108,51,7;127.0.0.1"
AddressExpression address
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T19:24:18+00:00Added an answer on May 14, 2026 at 7:24 pm

    Here’s mine in 63 lines of F# (including the one test case); it worked the first time, and feels pretty readable to me. It’s a typical parser-followed-by-pretty-printer. What do we think?

    type IPs = IP[]
    and IP = IP of OrParts * OrParts * OrParts * OrParts
    and OrParts = Or of Part[]
    and Part = Num of int | Range of int * int
    
    let Valid(x) = if x < 0 || x > 255 then failwithf "Invalid number %d" x
    
    let rec parseIPs (s:string) =
        s.Split [|';'|] |> Array.map parseIP
    and parseIP s =
        let [|a;b;c;d|] = s.Split [|'.'|]
        IP(parseOrParts a, parseOrParts b, parseOrParts c, parseOrParts d)
    and parseOrParts s =
        Or(s.Split [|','|] |> Array.map parsePart)
    and parsePart s =
        if s.Contains("-") then
            let [|a;b|] = s.Split [|'-'|]
            let x,y = int a, int b
            Valid(x)
            Valid(y)
            if x > y then failwithf "Invalid range %d-%d" x y
            Range(x, y)
        else
            let x = int s
            Valid(x)
            Num(x)
    
    let rec printIPsAsRegex ips =
        let sb = new System.Text.StringBuilder()
        let add s = sb.Append(s:string) |> ignore
        add "("
        add(System.String.Join("|", ips |> Array.map printIPAsRegex))
        add ")"
        sb.ToString()
    and printIPAsRegex (IP(a, b, c, d)) : string =
        let sb = new System.Text.StringBuilder()
        let add s = sb.Append(s:string) |> ignore
        add "("
        printPartsAsRegex add a
        add "."
        printPartsAsRegex add b
        add "." 
        printPartsAsRegex add c
        add "."
        printPartsAsRegex add d
        add ")"
        sb.ToString()
    and printPartsAsRegex add (Or(parts)) =
        match parts with
        | [| Num x |] -> // exactly one Num
            add(string x)
        | _ ->
            add "("
            add(System.String.Join("|", parts |> Array.collect (function
                    | Num x -> [| x |]
                    | Range(x,y) -> [| x..y |])
                |> Array.map (fun x -> x.ToString())))
            add ")"
    
    let Main() =
        let ips = parseIPs "192.168.0.250,244-248,108,51,7;127.0.0.1"
        printfn "%s" (printIPsAsRegex ips)
    Main()                
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.