Search code examples
regexultraedit

RegEx - How to select the second comma and everything after it


I'm using UltraEdit. I have a text file that contains strings like this

Workspace\\Trays\\Dialogs\\Components, Expand, kThisComputerOnly, P_BOOLEAN },
WebCommonDialog Sign_Out, Left, kThisComputerOnly, P_INTEGER_RANGE(0, 4096) },
ThreeDTextDlg, x, kThisComputerOnly, P_INTEGER_RANGE(0, 4096) },
Preferences\\Graphics, CtxDbgMaxGLVersionMajor, kThisComputerOnly, P_INTEGER },

UltraEdit allows PERL, UNIX and UltraEdit style RegEx. I need to select the second comma and everything to the end of the line and delete it.

Using regexpal.com I've tried several different approaches but can't figure it out.

/,\s.+/ selects the first comma
/[,]\s.+/ same as above

I can't figure out how to select the second command and beyond.

I have also search StackOverflow and found several examples but couldn't change them to work for me.

Thanks.


Solution

  • You may use a Perl regex option with the following pattern:

    ^([^,]*,[^,]*),.*
    

    and replace with \1.

    See the regex demo.

    Details:

    • ^ - start of string
    • ([^,]*,[^,]*) - Group 1 (later referred to with \1 backreference from the replacement pattern):
      • [^,]* - any 0+ chars other than a comma (to prevent overflowing across lines, add \n\r into the negated character class - [^,\n\r]*)
      • , - a comma
      • [^,]* - any 0+ chars other than a comma
    • , - a comma
    • .* - any 0+ chars other than line break chars as many as possible