Separate the house number from the street name: Difference between revisions

Content added Content deleted
m (→‎{{header|J}}: improve heuristics)
m (syntax highlighting fixup automation)
Line 49: Line 49:
{{trans|Nim}}
{{trans|Nim}}


<lang 11l>F separateHouseNumber(address)
<syntaxhighlight lang="11l">F separateHouseNumber(address)
V fields = address.split_py()
V fields = address.split_py()
V last = fields.last
V last = fields.last
Line 102: Line 102:
L(address) Addresses
L(address) Addresses
V (street, house) = separateHouseNumber(address)
V (street, house) = separateHouseNumber(address)
print(street.rjust(22)‘ ’(I !house.empty {house} E ‘(none)’))</lang>
print(street.rjust(22)‘ ’(I !house.empty {house} E ‘(none)’))</syntaxhighlight>


{{out}}
{{out}}
Line 144: Line 144:


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
<lang scheme>
<syntaxhighlight lang="scheme">
(lib 'struct)
(lib 'struct)
(lib 'sql)
(lib 'sql)
Line 208: Line 208:
"Schmidener Weg 3"
"Schmidener Weg 3"
"Karl-Weysser-Str. 6"))
"Karl-Weysser-Str. 6"))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 250: Line 250:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Seperate house number and street in Dutch addresses. Nigel Galloway: September 23rd., 2021
// Seperate house number and street in Dutch addresses. Nigel Galloway: September 23rd., 2021
let fN g=let n=System.Text.RegularExpressions.Regex.Match(g,@"(\s\d+[-/]\d+)|(\s(?!1940|1945)\d+[a-zI. /]*\d*)$") in if n.Success then Some(g.[0..n.Index],n.Value) else None
let fN g=let n=System.Text.RegularExpressions.Regex.Match(g,@"(\s\d+[-/]\d+)|(\s(?!1940|1945)\d+[a-zI. /]*\d*)$") in if n.Success then Some(g.[0..n.Index],n.Value) else None
Line 256: Line 256:
printfn " Street Number\n ______ ______"
printfn " Street Number\n ______ ______"
td|>List.iter(fun g->match fN g with Some(n,g)->printfn $"%27s{n.Trim()} %s{g}" |_->printfn $"FAILED %s{g}")
td|>List.iter(fun g->match fN g with Some(n,g)->printfn $"%27s{n.Trim()} %s{g}" |_->printfn $"FAILED %s{g}")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 299: Line 299:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 375: Line 375:
fmt.Printf("%-22s %s\n", street, house)
fmt.Printf("%-22s %s\n", street, house)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 421: Line 421:
{{Works with|GHC|7.8.3}}
{{Works with|GHC|7.8.3}}


<lang haskell>{-# LANGUAGE OverloadedStrings #-}
<syntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings #-}


{- Recommended package versions to use:
{- Recommended package versions to use:
Line 521: Line 521:
Right r -> return r
Right r -> return r
pairs <- mapM (splitAddress rx) testSet
pairs <- mapM (splitAddress rx) testSet
mapM_ T.putStrLn $ formatPairs pairs</lang>
mapM_ T.putStrLn $ formatPairs pairs</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 564: Line 564:


'''Solution''':
'''Solution''':
<lang j>special=: '4',.'012345'
<syntaxhighlight lang="j">special=: '4',.'012345'
digit=: '0123456789'
digit=: '0123456789'
nope=: {{>./({.I.y=' '),1+I. special +./@:(E."1) y}}
nope=: {{>./({.I.y=' '),1+I. special +./@:(E."1) y}}
here=: {{I.1,~y e.digit}}
here=: {{I.1,~y e.digit}}
din5008=: ({.;}.)~ here {.@#~ nope < here</lang>
din5008=: ({.;}.)~ here {.@#~ nope < here</syntaxhighlight>


Sample data:
Sample data:


<lang j> sampledata=: ];._2 noun define
<syntaxhighlight lang="j"> sampledata=: ];._2 noun define
Straat 12
Straat 12
Straat 12 II
Straat 12 II
Line 606: Line 606:
Schmidener Weg 3
Schmidener Weg 3
Karl-Weysser-Str. 6
Karl-Weysser-Str. 6
)</lang>
)</syntaxhighlight>


'''Example''':<lang j> din5008"1 sampledata
'''Example''':<syntaxhighlight lang="j"> din5008"1 sampledata
┌───────────────────┬────────────────┐
┌───────────────────┬────────────────┐
│Straat │12 │
│Straat │12 │
Line 673: Line 673:
├───────────────────┼────────────────┤
├───────────────────┼────────────────┤
│Karl-Weysser-Str. │6 │
│Karl-Weysser-Str. │6 │
└───────────────────┴────────────────┘</lang>
└───────────────────┴────────────────┘</syntaxhighlight>


=={{header|jq}}==
=={{header|jq}}==
Line 685: Line 685:
"(?<s>.*) (?<n> (\\s\\d+[-/]\\d+) | (\\s(?!1940|1945)\\d+[a-zI. /]*\\d*)$ | \\d+\\['][40|45]$ )"
"(?<s>.*) (?<n> (\\s\\d+[-/]\\d+) | (\\s(?!1940|1945)\\d+[a-zI. /]*\\d*)$ | \\d+\\['][40|45]$ )"
</pre>
</pre>
<lang jq>def regex:
<syntaxhighlight lang="jq">def regex:
"^ (?<s>.*?) \\s+"
"^ (?<s>.*?) \\s+"
+ " (?<n>\\d* ( \\-|\\/)? \\d*"
+ " (?<n>\\d* ( \\-|\\/)? \\d*"
Line 700: Line 700:
| .n |= if . == "" or . == null then "(none)" else . end ;
| .n |= if . == "" or . == null then "(none)" else . end ;


def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;</lang>
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;</syntaxhighlight>
'''The Task'''
'''The Task'''
<lang jq>def addresses:
<syntaxhighlight lang="jq">def addresses:
"Plataanstraat 5",
"Plataanstraat 5",
"Straat 12",
"Straat 12",
Line 745: Line 745:
| "\(.s|lpad(22)) \(.n)" ) ;
| "\(.s|lpad(22)) \(.n)" ) ;


task</lang>
task</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 787: Line 787:
=={{header|Julia}}==
=={{header|Julia}}==
Uses the regex from the Perl version.
Uses the regex from the Perl version.
<lang julia>const regex = r"""^ (.*?) \s+
<syntaxhighlight lang="julia">const regex = r"""^ (.*?) \s+
(
(
\d* (\-|\/)? \d*
\d* (\-|\/)? \d*
Line 837: Line 837:
end
end
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Plataanstraat 5 split as street => Plataanstraat, number => 5
Plataanstraat 5 split as street => Plataanstraat, number => 5
Line 875: Line 875:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.0.6
<syntaxhighlight lang="scala">// version 1.0.6


val r = Regex("""\s+""")
val r = Regex("""\s+""")
Line 939: Line 939:
println("${street.padEnd(22)} ${if (house != "") house else "(none)"}")
println("${street.padEnd(22)} ${if (house != "") house else "(none)"}")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 982: Line 982:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Go}}
{{trans|Go}}
<lang Nim>import strutils except align
<syntaxhighlight lang="nim">import strutils except align
from unicode import align
from unicode import align


Line 1,037: Line 1,037:
for address in Addresses:
for address in Addresses:
let (street, house) = address.separateHouseNumber()
let (street, house) = address.separateHouseNumber()
echo street.align(22), " ", if house.len != 0: house else: "(none)"</lang>
echo street.align(22), " ", if house.len != 0: house else: "(none)"</syntaxhighlight>


{{out}}
{{out}}
Line 1,077: Line 1,077:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>@addresses = (
<syntaxhighlight lang="perl">@addresses = (
'Plataanstraat 5', 'Straat 12', 'Straat 12 II', 'Dr. J. Straat 12',
'Plataanstraat 5', 'Straat 12', 'Straat 12 II', 'Dr. J. Straat 12',
'Dr. J. Straat 12 a', 'Dr. J. Straat 12-14', 'Laan 1940 – 1945 37', 'Plein 1940 2',
'Dr. J. Straat 12 a', 'Dr. J. Straat 12-14', 'Laan 1940 – 1945 37', 'Plein 1940 2',
Line 1,097: Line 1,097:
$]x;
$]x;
$number ? printf "%-26s\t%s\n", ($street, $number) : ($_, "\t(no match)");
$number ? printf "%-26s\t%s\n", ($street, $number) : ($_, "\t(no match)");
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:35ex">Plataanstraat 5
<pre style="height:35ex">Plataanstraat 5
Line 1,135: Line 1,135:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|Go}}
{{trans|Go}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">isDigit</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">isDigit</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">)</span>
Line 1,211: Line 1,211:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,253: Line 1,253:
=={{header|Python}}==
=={{header|Python}}==
<!-- ?? missing code ?? -->
<!-- ?? missing code ?? -->
<lang python>
<syntaxhighlight lang="python">
Plataanstraat 5 split as (Plataanstraat, 5)
Plataanstraat 5 split as (Plataanstraat, 5)
Straat 12 split as (Straat, 12)
Straat 12 split as (Straat, 12)
Line 1,286: Line 1,286:
Marktplatz 31 split as (Marktplatz, 31)
Marktplatz 31 split as (Marktplatz, 31)
Schmidener Weg 3 split as (Schmidener Weg, 3)
Schmidener Weg 3 split as (Schmidener Weg, 3)
Karl-Weysser-Str. 6 split as (Karl-Weysser-Str., 6)''')</lang>
Karl-Weysser-Str. 6 split as (Karl-Weysser-Str., 6)''')</syntaxhighlight>


=={{header|Racket}}==
=={{header|Racket}}==
Line 1,292: Line 1,292:
Same as other regexp-splittings on this page. (I don't see much point in this, but the related [[Starting_a_web_browser]] seems like a good idea.)
Same as other regexp-splittings on this page. (I don't see much point in this, but the related [[Starting_a_web_browser]] seems like a good idea.)


<lang racket>
<syntaxhighlight lang="racket">
#lang racket
#lang racket


Line 1,344: Line 1,344:
(cond [(splits-adressen str) => cdr]
(cond [(splits-adressen str) => cdr]
[else '???])))
[else '???])))
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,393: Line 1,393:
in places such as the U.S. where each block gets a hundred house numbers
in places such as the U.S. where each block gets a hundred house numbers
to play with, and there are cities with hundreds of blocks along a street.)
to play with, and there are cities with hundreds of blocks along a street.)
<lang perl6>say m[
<syntaxhighlight lang="raku" line>say m[
( .*? )
( .*? )


Line 1,405: Line 1,405:


$
$
] for lines;</lang>
] for lines;</syntaxhighlight>
{{out}}
{{out}}
<pre>「Plataanstraat 5」
<pre>「Plataanstraat 5」
Line 1,541: Line 1,541:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program splits an European mail address into an address and a house number. */
<syntaxhighlight lang="rexx">/*REXX program splits an European mail address into an address and a house number. */
!= '│' /*a pipe-ish symbol for $ concatenation*/
!= '│' /*a pipe-ish symbol for $ concatenation*/
$= "Plataanstraat 5" ! ,
$= "Plataanstraat 5" ! ,
Line 1,597: Line 1,597:
(datatype(e, 'N') & pl & \verify("'", p, "M")) then s=s-1
(datatype(e, 'N') & pl & \verify("'", p, "M")) then s=s-1
if s==0 then s=n /*if no split, then relocate split to ∞*/
if s==0 then s=n /*if no split, then relocate split to ∞*/
return s /* [↑] indicate where to split the txt*/</lang>
return s /* [↑] indicate where to split the txt*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default (internal) input:}}
{{out|output|text=&nbsp; when using the default (internal) input:}}
<pre>
<pre>
Line 1,636: Line 1,636:


=={{header|Scala}}==
=={{header|Scala}}==
<lang Scala>object SplitHouseNumber extends App {
<syntaxhighlight lang="scala">object SplitHouseNumber extends App {
val extractor = new scala.util.matching.Regex( """(\s\d+[-/]\d+)|(\s(?!1940|1945)\d+[a-zI. /]*\d*)$|\d+\['][40|45]$""")
val extractor = new scala.util.matching.Regex( """(\s\d+[-/]\d+)|(\s(?!1940|1945)\d+[a-zI. /]*\d*)$|\d+\['][40|45]$""")


Line 1,679: Line 1,679:


adressen.foreach(s => println(f"$s%-25s split as ${splitsAdressen(s)}"))
adressen.foreach(s => println(f"$s%-25s split as ${splitsAdressen(s)}"))
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Plataanstraat 5 split as (Plataanstraat, 5)
<pre>Plataanstraat 5 split as (Plataanstraat, 5)
Line 1,717: Line 1,717:
=={{header|Sidef}}==
=={{header|Sidef}}==
{{trans|Raku}}
{{trans|Raku}}
<lang ruby>var re = %r[
<syntaxhighlight lang="ruby">var re = %r[
( .*? )
( .*? )
(?:
(?:
Line 1,736: Line 1,736:
warn "Can't parse: «#{line}»"
warn "Can't parse: «#{line}»"
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,776: Line 1,776:
=={{header|Tcl}}==
=={{header|Tcl}}==
{{trans|Scala}}
{{trans|Scala}}
<lang tcl>proc split_DE_NL_address {streetAddress} {
<syntaxhighlight lang="tcl">proc split_DE_NL_address {streetAddress} {
set RE {(?x)
set RE {(?x)
^ (.*?) (
^ (.*?) (
Line 1,829: Line 1,829:
lassign [split_DE_NL_address $streetAddress] str num
lassign [split_DE_NL_address $streetAddress] str num
puts "split <$streetAddress> as <$str> <$num>"
puts "split <$streetAddress> as <$str> <$num>"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,868: Line 1,868:


=={{header|TUSCRIPT}}==
=={{header|TUSCRIPT}}==
<lang tuscript>
<syntaxhighlight lang="tuscript">
$$ MODE DATA
$$ MODE DATA


Line 1,919: Line 1,919:


TRACE *output
TRACE *output
</syntaxhighlight>
</lang>
Output:
Output:
<pre style='height:30ex;overflow:scroll'>
<pre style='height:30ex;overflow:scroll'>
Line 1,963: Line 1,963:
{{libheader|Wren-pattern}}
{{libheader|Wren-pattern}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/pattern" for Pattern
<syntaxhighlight lang="ecmascript">import "/pattern" for Pattern
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 2,034: Line 2,034:
if (house == "") house = "(none)"
if (house == "") house = "(none)"
Fmt.print("$-22s $s", street, house)
Fmt.print("$-22s $s", street, house)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}