Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Need regex type expression in M code

Hi,

 

I have a column that contains string values like:

InputOutputComments
IAM 22052205 
SAP NO 2022 IY92022IY9 is not purely a numerical value so it shoudnt be returned


I just want to extract the numbers from these strings that are given in the output as above. How can i do it?

 

1 ACCEPTED SOLUTION
PhilipTreacy
Super User
Super User

Hi @Anonymous @Anonymous 

OK then try this.  Should work to extract any number of any length from a string as long as that number is delimited by spaces.

Download the PBIX file here

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8nT0VTAyMjBVitWJVgp2DFDw81cwMjAyUvCMtISLRUSAxcB8sGRUlAJQXCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Input = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each List.Sum(List.Transform(Text.Split([Input], " "), each try Number.From(_) otherwise 0)))
in
    #"Added Custom"

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


View solution in original post

7 REPLIES 7
PhilipTreacy
Super User
Super User

Hi @Anonymous 

Did this work for you?

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


PhilipTreacy
Super User
Super User

Hi @Anonymous @Anonymous 

OK then try this.  Should work to extract any number of any length from a string as long as that number is delimited by spaces.

Download the PBIX file here

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8nT0VTAyMjBVitWJVgp2DFDw81cwMjAyUvCMtISLRUSAxcB8sGRUlAJQXCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Input = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each List.Sum(List.Transform(Text.Split([Input], " "), each try Number.From(_) otherwise 0)))
in
    #"Added Custom"

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


PhilipTreacy
Super User
Super User

Hi @Anonymous  @Anonymous 

So you just want to extract a 4 digit number from within a string? What about bits like IY9 as comments?

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


PhilipTreacy
Super User
Super User

Hi @Anonymous 

Download this PBIX file with code

This code will do the trick

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8nT0VTAyMjBVitWJVgp2DFDw81cwMjAyUvCMtFSKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Input = _t]),
    #"Split Column by Delimiter" = Table.SplitColumn(Source, "Input", Splitter.SplitTextByCharacterTransition( {" "},  {"0".."9"})),
    #"Split Column by Delimiter1" = Table.SplitColumn(#"Split Column by Delimiter", "Input.2", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Input.2.1", "Input.2.2"}),
    #"Renamed Columns" = Table.RenameColumns(#"Split Column by Delimiter1",{{"Input.2.2", "Comments"}, {"Input.2.1", "Output"}})
in
    #"Renamed Columns"

 

 

split.png

 

Your result table above had IY9 in a Comments column so I wasn't sure if you wanted to keep that.  If you don't, just delete the column. 

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Anonymous
Not applicable

Hi Phil,

 

Appreciate your reponse.

However the code breaks when I add more strings. See below.

Hence I was looking for a more robust regex expression that would not fail in the future

 

Input:

leharkapil_1-1612434682566.png

 

Output:

 

 

leharkapil_0-1612434615504.png

 

If you could also explain the expressions you use, that would be a cherry on the cake!

 

Hi @Anonymous  @Anonymous 

If you just want the 4 digit number then this works.

Download sample PBIX file

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8nT0VTAyMjBVitWJVgp2DFDw81cwMjAyUvCMtISLRUSAxcB8sGRUlAJQXCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Input = _t]),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each Text.Middle([Input],Text.PositionOfAny([Input],{"0".."9"}),4))
in
    #"Added Custom"

 

fdig.png

The code uses Text.PositionOfAny to find the first occurrence of a numeric digit in the text.  This is fed into Text.Middle to extract the 4 characters from that point, giving you the 4 digit number.

Regards

Phil



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Anonymous
Not applicable

Hi Phil sorry for the late reply.

However the number can be as 1 digit and as long as 9 digits as well.

Its not the year, if you were confused by 2022

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.