+ Reply to Thread
Results 1 to 6 of 6

regexp character exclude \r and \n

Hybrid View

  1. #1
    Registered User
    Join Date
    12-25-2014
    Location
    Bourgogne, France
    MS-Off Ver
    2003
    Posts
    2

    regexp character exclude \r and \n

    Hello forum members,

    This is my 1st post, first of all I want to say hello to everyone here

    My problem is this:

    I would like to extract, from the whole string the string that immediately precedes the parentheses
    The cells contain several string separated by a carriage return – linefeed

    my data looks something like this - I Need an Output like this

    ELMS (United Autosports)
    IMSA (Action Express) ------> ELMS IMSA

    WRC (Hyundai) ------> WRC

    WEC (Toyota)
    Formula E (Renault)
    Day+Seb (Rebellion) -------> WEC Formula E Day+Seb

    NASCAR Cup
    (JTG Daugherty) -------> NASCAR Cup

    In the attached file, the values are written in column A starting at A2
    Very grateful for your help
    Option Explicit
    Sub test()
    Dim r As Range, i As Long
    Dim RegX As Object
        Set RegX = CreateObject("VBScript.RegExp")
        With RegX
            .Pattern = "([^\(]+)? ?\(([^\(\)]+)\)"
            .Global = True
        End With
        With Sheets("Liste")
            For Each r In .Range("a2", .Range("a" & .Rows.Count).End(xlUp))
                If RegX.test(r.Value) Then
                    For i = 0 To RegX.Execute(r.Value).Count - 1
                        r(, i + 3).Value = RegX.Execute(r.Value)(i).submatches(0)
                    Next
                End If
            Next
        End With
    End Sub
    klin89
    Attached Files Attached Files
    Last edited by klin89; 04-19-2017 at 09:38 PM.

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: regexp character exclude \r and \n

    Hi Klin89 and welcome to the forum.

    I expected to see some blanks in your cells but didn't. See if this simple formula works instead of any VBA.

    =TRIM(LEFT(A2,FIND("(",A2)-1))

    klin89 answer1.xls
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

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

    Re: regexp character exclude \r and \n

    Is this how you wanted?
    Sub test()
        Dim r As Range, i As Long
        Dim RegX As Object
        Set RegX = CreateObject("VBScript.RegExp")
        With RegX
            .Pattern = "\([^)]+\)\n*"
            .Global = True
        End With
        With Sheets("Liste")
            For Each r In .Range("a2", .Range("a" & .Rows.Count).End(xlUp))
                r(, 3) = RegX.Replace(r.Value, "")
            Next
        End With
    End Sub

  4. #4
    Registered User
    Join Date
    12-25-2014
    Location
    Bourgogne, France
    MS-Off Ver
    2003
    Posts
    2

    Re: regexp character exclude \r and \n

    Hello everyone,

    Thanks for your reply, however, I had to express myself very badly
    The lines are separated by a carriage return in the cell.
    I need a macro to keep only the discipline that precedes the parentheses, without leaving space at the end of the string

    The picture below shows the desired result

    photo1.JPG

    klin89

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

    Re: regexp character exclude \r and \n

    Then change the pattern in your code to
            .Pattern = "([^\(\n]+)?[ \n]+\(([^\(\)]+)\)"

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: regexp character exclude \r and \n

    OK - try this code to do what you want. Run it in the attached...

    Sub Klin89()
        Dim LastRow As Double
        Dim RowCtr As Double
        Dim Ary As Variant
        Dim AryCtr As Double
        LastRow = Cells(Rows.Count, "A").End(xlUp).Row
        
        For RowCtr = 2 To LastRow
        
            Ary = Split(Cells(RowCtr, "A"), Chr(10))
            For AryCtr = 0 To UBound(Ary)
                If InStr(Ary(AryCtr), "(") > 0 Then
                    Cells(RowCtr, AryCtr + 3) = Left(Ary(AryCtr), InStr(Ary(AryCtr), "(") - 1)
                Else
                    Cells(RowCtr, AryCtr + 3) = Ary(AryCtr)
                End If
            Next AryCtr
        Next RowCtr
    
    End Sub
    VBA Split vbNewLine to Rows.xls

+ 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. RegExp Pattern to remove special character
    By BS Singh in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-15-2014, 12:12 AM
  2. Formatting using Regular Expression (RegExp)
    By vnzerem in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-13-2014, 10:03 AM
  3. [SOLVED] VBA RegExp
    By vnzerem in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-11-2014, 05:45 PM
  4. [SOLVED] RegExp function
    By pkiula in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 08-17-2013, 06:22 AM
  5. Error with Set RegExp equal to New RegExp vba
    By bdb1974 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-22-2011, 05:28 PM
  6. RegExp
    By YBrazeau in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-13-2010, 07:51 PM
  7. RegExp Pattern
    By MBCMDR in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 12-08-2009, 02:09 PM

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