+ Reply to Thread
Results 1 to 22 of 22

RegEx pattern

Hybrid View

capson RegEx pattern 02-14-2015, 03:43 PM
Pepe Le Mokko Re: RegEx pattern 02-14-2015, 03:45 PM
capson Re: RegEx pattern 02-14-2015, 03:51 PM
Pepe Le Mokko Re: RegEx pattern 02-14-2015, 03:54 PM
capson Re: RegEx pattern 02-14-2015, 03:57 PM
AB33 Re: RegEx pattern 02-14-2015, 04:28 PM
capson Re: RegEx pattern 02-14-2015, 05:50 PM
AB33 Re: RegEx pattern 02-14-2015, 05:55 PM
capson Re: RegEx pattern 02-14-2015, 05:58 PM
apo Re: RegEx pattern 02-14-2015, 06:19 PM
xladept Re: RegEx pattern 02-14-2015, 06:52 PM
apo Re: RegEx pattern 02-14-2015, 06:57 PM
xladept Re: RegEx pattern 02-14-2015, 07:00 PM
jindon Re: RegEx pattern 02-14-2015, 07:08 PM
AB33 Re: RegEx pattern 02-14-2015, 07:12 PM
jindon Re: RegEx pattern 02-14-2015, 07:58 PM
capson Re: RegEx pattern 02-15-2015, 12:04 PM
jindon Re: RegEx pattern 02-15-2015, 12:13 PM
xladept Re: RegEx pattern 02-15-2015, 02:53 PM
apo Re: RegEx pattern 02-15-2015, 03:57 PM
capson Re: RegEx pattern 02-15-2015, 04:38 PM
jindon Re: RegEx pattern 02-15-2015, 06:57 PM
  1. #1
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    RegEx pattern

    Very new to RegEx,
    I have in cells in a column like:
    Stuff, Other (please specify)
    I want
    Stuff
    I have tried patterns like:
    "(, Other\(please specify\))"
    or
    "(,\sOther\(please specify\))"
    Not getting it

    Thanks

  2. #2
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,616

    Re: RegEx pattern

    What is the relation to Excel?

  3. #3
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Re: RegEx pattern

    I am using it in VBA

  4. #4
    Forum Expert Pepe Le Mokko's Avatar
    Join Date
    05-14-2009
    Location
    Belgium
    MS-Off Ver
    O365 v 2402
    Posts
    13,616

    Re: RegEx pattern

    Thanks. VBA is not my forte. Didn't know that. Learned something today

  5. #5
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Re: RegEx pattern

    I am using like this
    Sub RemoveRegEx_n()
    Dim x
    Dim R As Object
    Dim ws As Worksheet
    Dim rng1 As Range
    Dim lngRow As Long
    
         Set R = CreateObject("vbscript.regexp")
         Set ws = ThisWorkbook.Sheets("XXX")
         With R
           .Global = True
           .Pattern = (,\sOther\(please specify\))
           .ignorecase = True
         End With
         Application.ScreenUpdating = False
         Set rng1 = ws.Range("A1:A" & ws.Cells(Rows.Count, 1).End(xlUp).Row)
         x = rng1.Value2
         For lngRow = 1 To UBound(x, 1)
           x(lngRow, 1) = R.Replace(x(lngRow, 1), vbNullString)
         Next lngRow
         rng1.Value2 = x
         Application.ScreenUpdating = True
    End Sub

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: RegEx pattern

    If you just the word stuff
    "[Stuff]"
    Or


    "[Stuff|sOther|(please specify)]"
    Last edited by AB33; 02-14-2015 at 04:38 PM.

  7. #7
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Re: RegEx pattern

    Apologies about my sloppy question, I have cells like:
    Civic Engagement, Community Building, Education, Leadership Development, Other (please specify)
    or
    Arts, Culture, and Humanities, Community Building, Government and Public Administration, Spirituality/Religion, Other (please specify)
    There are more possibilities but all end with
    , Other (please specify)

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: RegEx pattern

    What do you want to return or extract?

  9. #9
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Re: RegEx pattern

    From the above
    Civic Engagement, Community Building, Education, Leadership Development
    Arts, Culture, and Humanities, Community Building, Government and Public Administration, Spirituality/Religion
    I want to remove the
    , Other (please specify)
    from the ends of all the strings in my range

    Thanks

  10. #10
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: RegEx pattern

    Hi..

    Rather than using Regex and most probably a loop.. try this..

    Private Sub CommandButton21_Click()
        With Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
            .Value = Application.Transpose(Split(Replace(Join(Application.Transpose(.Value), vbCrLf), ", Other (please specify)", ""), vbCrLf))
        End With
    End Sub

  11. #11
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: RegEx pattern

    Nevermind - It didn't work
    Last edited by xladept; 02-14-2015 at 06:58 PM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  12. #12
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: RegEx pattern

    lol.. mm.. yeap.. I may have gone a bit overboard on that one..

  13. #13
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: RegEx pattern

    No - you didn't go overboard - my condensation worked when there was just one line - but, you're convolutions were necessary!

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: RegEx pattern

    Why RegExp?

    Just replace ", Other (please specify)" with "" should do....

    If you insist
    Sub test()
        Dim r As Range
        With CreateObject("VBScript.RegExp")
            .Pattern = "(.+)(?=(, Other \(please specify\)))"
            For Each r In Range("a1", Range("a" & Rows.Count).End(xlUp))
                If .test(r.Value) Then r.Value = .Execute(r.Value)(0)
            Next
        End With
    End Sub

  15. #15
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: RegEx pattern

    Jindon's code is much better than mine and works in all circumstances, but I just post it.
    Attached Files Attached Files
    Last edited by AB33; 02-14-2015 at 07:17 PM.

  16. #16
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: RegEx pattern

    Ah, I just remembered your thread.

    http://www.excelforum.com/excel-prog...g-nothing.html

    For the learning purpose...

    You want to remove

    Stuff, Other (please specify)

    So the patter should be

    "(, Other \(please specify\))"

    You missed a space after "Other"...

  17. #17
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Re: RegEx pattern

    Thank you all for your input I greatly appreciate the different approaches, though I admit to being a bit lost on apo's solution

    I was also trying to use the code below, but it was not replaceing the (please specify) in ", Other(please specify)", I could replace ", Other" then "(please specify)" but not ", Other(please specify)" in one go
    Sub ReplaceUsingArray()
    Dim myBadChars As Variant
    Dim ws As Worksheet
    Dim Rng As Range
    Dim iCtr As Long
      
      Set ws = ThisWorkbook.Sheets("XXX")
      Set Rng = ws.Range("D2:D" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
    
      myBadChars = Array(", Other(please specify)")
    
      For iCtr = LBound(myBadChars) To UBound(myBadChars)
          Rng.Cells.Replace what:=myBadChars(iCtr), Replacement:="", _
                              LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False
      Next iCtr
    
    End Sub
    Last edited by capson; 02-15-2015 at 12:08 PM.

  18. #18
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: RegEx pattern

    Quote Originally Posted by capson View Post
    I have in cells in a column like:
    Stuff, Other (please specify)
    I want
    Stuff
    As I mentioned already, you missed a space after "Other"

    You need to replace "Other (please specify)"

  19. #19
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: RegEx pattern

    Hi capson,

    The beauty of APO's routine is that it does all the replacements at once - so it should be quickest.

    Jindon's routine is vintage Jindon - elegantly cryptic - but it processes each phrase, not the whole thing all at once.

    Jindon initially pointed out that you only needed a simple replacement - this routine processes each phrase so it can't be as quick as APO's routine:

    Sub DumpPhrase(): Dim R As Range: For Each R In Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
    R.Value = Replace(R.Value, ", Other (please specify)", ""): Next: End Sub

  20. #20
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: RegEx pattern

    Hi capson..

    though I admit to being a bit lost on apo's solution
    I will try to break it down for you so you might understand what i was thinking..

    Note to anyone else: If any of the following (technically or terminology) is incorrect... please let me know..

    First.. try stepping through this code.. its' basically the same as the 1 line of code but I have separated it so each line does a specific thing..

    IMPORTANT.. make sure your "Locals" window is open.

    In the VB Editor.. go to "View" and enable the "Locals" window.

    Private Sub CommandButton1_Click()
    Dim a, b, c, d, e, f
        With Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row)
            a = .Value
            b = Application.Transpose(.Value)
            c = Join(Application.Transpose(.Value), vbCrLf)
            d = Replace(Join(Application.Transpose(.Value), vbCrLf), ", Other (please specify)", "")
            e = Split(Replace(Join(Application.Transpose(.Value), vbCrLf), ", Other (please specify)", ""), vbCrLf)
            f = Application.Transpose(Split(Replace(Join(Application.Transpose(.Value), vbCrLf), ", Other (please specify)", ""), vbCrLf))
            .Value = Application.Transpose(Split(Replace(Join(Application.Transpose(.Value), vbCrLf), ", Other (please specify)", ""), vbCrLf))
        End With
    End Sub
    You will see something like this..

    Locals.png

    Explanation:
    a = Shows what the values would look lke when put into a 2 Dimensional array.

    b = Transpose the values from the array into a 1 Dimensional array. This is so we can use the Join command next.

    c = Join all the elements in the 1 Dimensional array (by the vbcrlf delimiter) into 1 long string.

    d = Replace all instances of ", Other (please specify)" with "" in the long string.

    e = Use the Split command (using the vbcrlf again as the delimter) to separate the lines into tseparate elements of a 1 Dimensional array.

    f = Transpose it back to a 2 Dimensional array so it can written to the sheet correctly.

    I hope that helps..

  21. #21
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Re: RegEx pattern

    Wow, that helped a lot, I think I actually understood it!!!

  22. #22
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,834

    Re: RegEx pattern

    You are all misunderstading what I offered at the first place, REPLACE

    sub test
    columns(1).replace ", Other (please specify)","",2
    end sub

    Or even

    sub test2()
        [a1:a1000] = [if(a1:a1000<>"",substitute(a1:a1000,", Other (please specify)",""),"")]
    end sub
    Last edited by jindon; 02-15-2015 at 07:06 PM. Reason: fixed a Type

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] RegEx pattern, code exacuting but doing nothing
    By capson in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-23-2015, 03:48 PM
  2. Regex: Matching pattern to split a cell data
    By DavidRoger in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-01-2014, 07:31 AM
  3. Regex - establishing a pattern
    By twckfa16 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 05-30-2014, 11:06 AM
  4. [SOLVED] RegEx pattern to extract multiplelines title in TXT file
    By p24leclerc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-06-2014, 08:39 PM
  5. Need a new regex .pattern for comparison code
    By Swiss Cheese in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-06-2012, 09:16 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1