Last active
June 16, 2023 07:46
-
-
Save Hugoberry/4948d96b45d6799c47b4b9fa1b08eadf to your computer and use it in GitHub Desktop.
Power Query regex example from http://www.thebiccountant.com/2018/04/25/regex-in-power-bi-and-power-query-in-excel-with-java-script/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let | |
fx=(x,y)=> | |
Web.Page( | |
"<script> | |
var x="&x&"; | |
var y=new RegExp('"&y&"','g'); | |
var b=x.match(y); | |
document.write(b); | |
</script>")[Data]{0}[Children]{0}[Children]{1}[Text]{0} | |
in | |
fx("""hello012中国1235""","\\d+") |
You can shrink down the whole thing above to just
(text, pattern) => Web.Page("<script>document.write(new RegExp('"& pattern &"').test('"& text &"'))</script>")[Data]{0}[Children]{0}[Children]{1}[Text]{0}
this only gets you a true/false output
My take on the snippet, ready to paste to as new query:
let fx=(text,regex)=>
Web.Page(
"<script>
var x='"&text&"';
var y=new RegExp('"®ex&"','g');
var b=x.match(y);
document.write(b);
</script>")[Data]{0}[Children]{0}[Children]{1}[Text]{0}
in
fx
basically it's the same, it's only easier to use if you don't know power query :)
edit: check the performance of this solution, I think this it is super slow for bigger data.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can shrink down the whole thing above to just
(text, pattern) => Web.Page("<script>document.write(new RegExp('"& pattern &"').test('"& text &"'))</script>")[Data]{0}[Children]{0}[Children]{1}[Text]{0}
I also recommend using this as a function that you call separately, as it'll keep things way cleaner