Isolate a value (text) in a String

EuEdu

New Member
Joined
May 8, 2019
Messages
4
Hi, all,

I need just a certain data from a text, but I'm having a great problem trying to get only what I need when a pattern isn't followed.


for example:

"dsadkjlk / lkadjs dl [sadasd] kjdasj [dasldkj / sdadas / dsadas/ dasdasd] adsdasd / dasds / asdasd"


In this text I need only what is inside the [*/*/*/*], ignoring everything else. My macro can do it when there is no other [ or ] or / writen, but in the case above it doesn't work.

I'm sure there is a method I don't know for that, but after hours and hours trying, I didn't manage to solve this.

Can anyone give me a light?

Very thanks!
 

Excel Facts

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

Code:
Function ExtractIt(s As String)
Dim RX As Object: Set RX = CreateObject("VBScript.Regexp")
Dim pat$: pat = "\[(\w+\s\/).*\]"


With RX
    .ignorecase = True
    .Global = True
    .Pattern = pat
    Set matches = .Execute(s)
End With


ExtractIt = matches(0)


End Function
 
Upvote 0
Here is another way to write the ExtractIt function that lrobbo314 posted but without using Regular Expressions...
Code:
Function ExtractIt(s As String) As String
  Dim X As Long, Arr As Variant
  Arr = Split(Replace(s, "[", "]"), "]")
  For X = 0 To UBound(Arr)
    If Arr(X) Like "*/*/*/*" Then
      ExtractIt = "[" & Arr(X) & "]"
      Exit For
    End If
  Next
End Function

Note to lrobbo314: I think your function returns the wrong value or errors out if the pattern does not exist (for example, "one[two/three]four")
 
Last edited:
Upvote 0
Here is another way to write the ExtractIt function that lrobbo314 posted but without using Regular Expressions...
Code:
Function ExtractIt(s As String) As String
  Dim X As Long, Arr As Variant
  Arr = Split(Replace(s, "[", "]"), "]")
  For X = 0 To UBound(Arr)
    If Arr(X) Like "*/*/*/*" Then
      ExtractIt = "[" & Arr(X) & "]"
      Exit For
    End If
  Next
End Function

Note to lrobbo314: I think your function returns the wrong value or errors out if the pattern does not exist (for example, "one[two/three]four")

Oh, my.... this is beautiful!!!

Thanks, friend. Thanks!!!

I was trying solve this for days without success, trying a lot of more complex solutions. It is part os a huge macro I write and I wasn't able to isolate and think on this problem alone.

Thanks a lot!!!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,171
Members
453,021
Latest member
Justyna P

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