How to pass matches inside function

map (s:String, f:EReg ‑> String):String

Calls the function f for the substring of s which this EReg matches and replaces that substring with the result of f call.

The f function takes this EReg object as its first argument and should return a replacement string for the substring matched.

I use to process a token of strings between [square] [brackets] among other words.

I need to replace each token found with a different value, based on what the token between brackets is.

An ideal case for using EReg.result, which nicely informs me about what text has now been caught by the regular expression. Well… :frowning:

Unfortunately, the result field of EReg is private and therefore I can’t obtain, inside the function called by map, what token is being processed.

How else could I do this?

I guess I could use EReg.match and matched, although this loses a lot of the elegance of using map to process the string of tokens.

You can call the matched(group:Int) function on the f:EReg parameter of map to get any group, including the group 0 (the whole match).

Ay ay ay @ibilon :slight_smile:. I should not trust the docs so much eh? :slight_smile:

I thought matched only works after match, as written in the EReg API:

This method should only be called after this.match or this.matchSub , and then operates on the String of that operation.

But deep inside, I was rather stupefied. How could something like this NOT be there!? I was thinking “this MUST work somehow, but maybe I’m missing some serious design consideration why it doesn’t”.

Thank you :smiley:

Well, the docs technically aren’t wrong - map() does do matches using matchSub() under the hood. :wink:

Yah yah yah :smiley: I also realized that as soon as @ibilon’s answer came in. But come on, this thing should be mentioned in the docs more clearly. I think I could make a PR for this right? Docs get built once the PR is approved?

PR’s are always welcome!

For the curious:
API docs changes, when merged, will get updated on automatically.
Official releases will have its own version number (like top level - Haxe 3.4.7 API) to have every version available.

For the real curious:
Each Haxe build outputs documentation xml for the standard library, it uses -xml -D doc-gen to generate the xmls. These files are automatically committed to this location at master · HaxeFoundation/ · GitHub
When there is a commit in this repo, a tool there runs dox (documentation generator) for each version, and puts that online.

1 Like