Regex: Missing one more pattern for split data

DavidRoger

Board Regular
Joined
Oct 2, 2011
Messages
135
Hi all,

Basically I want to split data into each cell just the number without alphabet and special character.

The data I want to split is:
a=1,995.000 b=2,001.000 c=1,994.000 d=1,996.000 e=1,281

Currently I have a set of code. New to regex, I need some help with the pattern.

The code work well with the above data. But cant split if the data doesn't have "," or ".".
ie, e=999. Search and trying a lot, I guess if the .pattern can be fixed then the code is complete.

The code is as below:

Code:
Sub onerowsplit()
    Dim i As Long, r As Range
        With CreateObject("VBScript.RegExp")
             .Pattern = "\d+[\,\.](\d+)?"
             .Global = True
          For Each r In Range(Range("k10").Value2)
             If .test(r.Value) Then
                For i = 0 To .Execute(r.Value).Count - 1
                    r(, i - 5).Value = Format(.Execute(r.Value)(i), "#")
                Next
             End If
          Next
        End With
End Sub

Please put the data in column K.
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Try

.Pattern = "\d+\,?\d*\.?\d*"

If not, please provide a couple of examples where it fails together with the expected results.
 
Upvote 0
Actually have a different pattern here, and a slightly changed code suggestion to save executing the RexEx pattern search multiple times for the one piece of data.
My pattern this time is based on your sample data showing that each "number" is followed by either a space or the end of the string.
I've also removed the If .Test() .. line as it is not required with this structure.
Rich (BB code):
Sub onerowsplit()
  Dim i As Long, r As Range, ary As Object
  
  With CreateObject("VBScript.RegExp")
    .Pattern = "(\d.*?)(?= |$)"
    .Global = True
    For Each r In Range(Range("k10").Value2)
        Set ary = .Execute(r.Value)
        For i = 0 To ary.Count - 1
          r(, i - 5).Value = Format(ary(i), "#")
        Next i
    Next r
  End With
End Sub
 
Upvote 0
Actually have a different pattern here, and a slightly changed code suggestion to save executing the RexEx pattern search multiple times for the one piece of data.
My pattern this time is based on your sample data showing that each "number" is followed by either a space or the end of the string.
I've also removed the If .Test() .. line as it is not required with this structure.
Rich (BB code):
Sub onerowsplit()
  Dim i As Long, r As Range, ary As Object
  
  With CreateObject("VBScript.RegExp")
    .Pattern = "(\d.*?)(?= |$)"
    .Global = True
    For Each r In Range(Range("k10").Value2)
        Set ary = .Execute(r.Value)
        For i = 0 To ary.Count - 1
          r(, i - 5).Value = Format(ary(i), "#")
        Next i
    Next r
  End With
End Sub

Hi Peter,

Let me study first. Probably I get back to you tomorrow.
 
Upvote 0
Actually have a different pattern here, and a slightly changed code suggestion to save executing the RexEx pattern search multiple times for the one piece of data.
My pattern this time is based on your sample data showing that each "number" is followed by either a space or the end of the string.
I've also removed the If .Test() .. line as it is not required with this structure.
Rich (BB code):
Sub onerowsplit()
  Dim i As Long, r As Range, ary As Object
  
  With CreateObject("VBScript.RegExp")
    .Pattern = "(\d.*?)(?= |$)"
    .Global = True
    For Each r In Range(Range("k10").Value2)
        Set ary = .Execute(r.Value)
        For i = 0 To ary.Count - 1
          r(, i - 5).Value = Format(ary(i), "#")
        Next i
    Next r
  End With
End Sub

I have tested it and explore it. Good one.

Thank you.

If you are free, please check the attachment through this link. Regex: Matching pattern to split a cell data [SOLVED]. You might want to have a look at AB33 version of pattern.
 
Upvote 0
I have tested it and explore it. Good one.

Thank you.
Glad to help.



If you are free, please check the attachment through this link. Regex: Matching pattern to split a cell data [SOLVED]. You might want to have a look at AB33 version of pattern.
As a general rule I choose not to download files from other sites. In any case that link just gives me a security warning stating that the site contains malware - confirming my reluctance to do such downloads.
 
Upvote 0
I notice another problem when apply some of the data. Some of the data are in this state, 0=1,995.000 b=2,001.000 c=1,994.000 0=1,996.000 e=1,281 "0=" is zero equal. As a result, it cant split probably.
 
Upvote 0
I notice another problem when apply some of the data. Some of the data are in this state, 0=1,995.000 b=2,001.000 c=1,994.000 0=1,996.000 e=1,281 "0=" is zero equal. As a result, it cant split probably.
I'm not quite sure where that data sample ends or what the expected results are?
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,685
Members
448,977
Latest member
dbonilla0331

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top