Skip to content

Commit 69139f7

Browse files
committed
add find-all-floats example
1 parent c1dd690 commit 69139f7

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/alexflint/go-restructure"
7+
)
8+
9+
var src = `
10+
The US economy went through an economic downturn following the financial
11+
crisis of 2007–08, with output as late as 2013 still below potential
12+
according to the Congressional Budget Office.[57] The economy, however,
13+
began to recover in the second half of 2009, and as of November 2015,
14+
unemployment had declined from a high of 10% to 5%; the government's
15+
broader U-6 unemployment rate, which includes the part-time underemployed,
16+
was 9.8% (it had reached 16% in 2009).[13] At 11.3%, the U.S. has one of
17+
the lowest labor union participation rates in the OECD.[58] Households
18+
living on less than $2 per day before government benefits, doubled from
19+
1996 levels to 1.5 million households in 2011, including 2.8 million
20+
children.[59] The gap in income between rich and poor is greater in the
21+
United States than in any other developed country.[60] Total public and
22+
private debt was $50 trillion at the end of the first quarter of 2010,
23+
or 3.5 times GDP.[61] In December 2014, public debt was slightly more
24+
than 100% of GDP.[62] Domestic financial assets totaled $131 trillion
25+
and domestic financial liabilities totaled $106 trillion.[63]
26+
`
27+
28+
var floatRegexp = restructure.MustCompile(Float{}, restructure.Options{})
29+
30+
// Matches "123", "1.23", "1.23e-4", "-12.3E+5", ".123"
31+
type Float struct {
32+
Begin restructure.Pos
33+
Sign *Sign `?`
34+
Whole string `[0-9]*`
35+
Period struct{} `\.?`
36+
Frac string `[0-9]+`
37+
Exponent *Exponent `?`
38+
End restructure.Pos
39+
}
40+
41+
// Matches "+" or "-"
42+
type Sign struct {
43+
Ch string `[+-]`
44+
}
45+
46+
// Matches "e+4", "E6", "e-03"
47+
type Exponent struct {
48+
_ struct{} `[eE]`
49+
Sign *Sign `?`
50+
Num string `[0-9]+`
51+
}
52+
53+
func main() {
54+
var floats []Float
55+
floatRegexp.FindAll(&floats, src, -1)
56+
for _, f := range floats {
57+
fmt.Println(src[f.Begin:f.End])
58+
}
59+
}

0 commit comments

Comments
 (0)