RegEX text until first occurrence for character

Sgdva

Board Regular
Joined
Nov 19, 2014
Messages
139
Greetings!
As the title states, I would like how to define a pattern to make regex take the text until the first occurrence for a defined character.
IE:
The useful text is here 'this is garbage 'so is this 'and this is garbage too

I tried to define pattern as
.Pattern = ".*(?=')"

But this takes the last result as consideration, so for the example would return:
The useful text is here 'this is garbage 'so is this
I need "The useful text is here" only.
Any ideas on the pattern? :confused:
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
This is untested so beware use of "." in any pattern can have many pitfalls.

.pattern = ".*?(?= ')"
 
Upvote 0
You can more than likely do what you want without using RegExp, but it would help to know how you want to apply this (a function that returns a single result, a macro that will process an entire range, something else)?
 
Upvote 0
You can more than likely do what you want without using RegExp, but it would help to know how you want to apply this (a function that returns a single result, a macro that will process an entire range, something else)?
Just to show you what I was thinking about here...

As a function applied to a single String argument...
Code:
Function BeforeApostrophe(S As String) As String
  BeforeApostrophe = Trim(Split(S, "'")(0))
End Function

As a subroutine outputting the parsed text to Column B...
Code:
[table="width: 500"]
[tr]
	[td]Sub GetTextBeforeApostrophe()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Range("B1").Resize(LastRow) = Evaluate(Replace("IFERROR(IF(@="""","""",LEFT(@,FIND(""'"",@)-1)),"""")", "@", "A1:A" & LastRow))
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
You can more than likely do what you want without using RegExp, but it would help to know how you want to apply this (a function that returns a single result, a macro that will process an entire range, something else)?
Through some coding, I narrowed a string from vba with that format (code lines with comments) but, there may be 'comment 'additional comment, etc and I only need to evaluate the real code.

Just to show you what I was thinking about here...

As a function applied to a single String argument...
Code:
Function BeforeApostrophe(S As String) As String
  BeforeApostrophe = Trim(Split(S, "'")(0))
End Function

As a subroutine outputting the parsed text to Column B...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub GetTextBeforeApostrophe()
  Dim LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Range("B1").Resize(LastRow) = Evaluate(Replace("IFERROR(IF(@="""","""",LEFT(@,FIND(""'"",@)-1)),"""")", "@", "A1:A" & LastRow))
End Sub[/TD]
[/TR]
</tbody>[/TABLE]
Yeah, that may be an approach, but, using the first function, wouldn't be faster with regex using a test and if so then execute?
(I'm not quite sure if the difference is even noticeable, but, I wanted to try regex, though, yeah, this does what I want too).

Using regex function may be like follows :)

Code:
Function BeforeChr(iChr As String, iText as String) As String
Dim Regex as Object
   Set Regex = CreateObject("VBScript.RegExp")
    With Regex 
    .Pattern = ".*?(?= " & iChr  & ")"
    End With
   If Regex .Test(iText) Then
   BeforeChr= Regex .Execute(iText )(0)
   Else
   BeforeChr = iText
   End if
   Set Regex = Nothing
End Function
 
Last edited:
Upvote 0
RegEx is the slowest function in the world.

You could also use

Code:
if instr(strInp, "'") then strOut = left(strInput, instr(strInp, "'") - 1 else strOut = strInp
 
Last edited:
Upvote 0
Oh! I didn't know that about regex! -Though, yeah, there are some scenarios that I really need to use it or I would be using left, right, mid so much and complicated!-
At the end, I used Rick solution Mini Quote: Trim(Split(S, "'")(0)) , since it handles no "'" in the string too.
 
Upvote 0
RegEx is the slowest function in the world.

While I certainly agree that Regex is slower and the non-Regex solutions proposed are better suited for this task, I disagree and would state that Regex really isn't that much slower then native VBA string functions. I just ran a couple of tests and the Non-Regex Methods could do this task 100,000 times in .18 seconds while the Regex Method could do it in .80 seconds (it should be noted the Regex object was only created once). Again, I'm glad to see the OP is using a non-Regex approach to solve his issue. Simple code is generally better code.
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,178
Members
452,615
Latest member
bogeys2birdies

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