# Regular Expressions and For Statement With Counter

**URL:** <https://forums.fsharp.org/t/regular-expressions-and-for-statement-with-counter/2303>\
**Category:** General\
**Tags:** beginners\
**Created:** [July 17, 2022, 6:09pm UTC](https://forums.fsharp.org/t/regular-expressions-and-for-statement-with-counter/2303 "2022-07-17T18:09:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Americore](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/americore/32/1031_2.png) [@Americore](https://forums.fsharp.org/u/Americore)\
**Post date:** [July 17, 2022, 6:09pm UTC](https://forums.fsharp.org/t/regular-expressions-and-for-statement-with-counter/2303/1 "2022-07-17T18:09:52Z")

</div>

An expression like this works by itself:

```auto
 for i in 0..10 do
 printf "%d" i

```

What makes it work when one of these values is substituted by a variable preluded by another complex expression such as a regular expression?

```auto
let str = File.ReadAllText "input.csv"
let result = Regex.Match(str, "\d+").ToString()
 
 for i in 0..result do
 printf "%d" i 

```

---

<div class="post-metadata">

**Author:** ![ntwilson](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/ntwilson/32/984_2.png) [@ntwilson](https://forums.fsharp.org/u/ntwilson)\
**Post date:** [July 18, 2022, 12:26am UTC](https://forums.fsharp.org/t/regular-expressions-and-for-statement-with-counter/2303/2 "2022-07-18T00:26:35Z")

</div>

The error I get when I attempt to execute your code is

```auto
  for i in 0..result do
  ------------^^^^^^

stdin(14,13): error FS0001: This expression was expected to have type
    'int'
but here has type
    'string'

```

It’s pointing out that it can’t _implicitly_ convert a string (`result`) to an int, but it has to be an int for that loop to work. You’ll need to parse or otherwise convert `result` to an int for it to compile.

---

<div class="post-metadata">

**Author:** ![Americore](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/americore/32/1031_2.png) [@Americore](https://forums.fsharp.org/u/Americore)\
**Post date:** [July 18, 2022, 2:01am UTC](https://forums.fsharp.org/t/regular-expressions-and-for-statement-with-counter/2303/3 "2022-07-18T02:01:36Z")

</div>

Perfect. Thanks NT. This worked for me:

> let str = File.ReadAllText “input.csv”  
> let result = Regex.Match(str, “\d+”)  
> let Str2Int = Res.ToString() |\> int
> 
> for i in 0…Str2Int do  
> printfn “%A” i
