How to delete text after a specific character in each row



  • how to delete text after | in each row ( i just showed 3 rows for example )
    string1 | string2
    string4001 | string2668
    string201 | string202



  • @Rana-Jawad

    Do a regular expression replacement:

    Find-what box: ^([^|]+\|).+
    Replace-with box: \1
    Search mode: ☑ Regular expression -AND- ☐ . matches newline

    After doing this to your data, it results in:

    string1 |
    string4001 |
    string201 |
    

    The ^ in the regular expression anchors the search to the start of each line. Next one or more non-| characters are matched followed by a |. The characters on the line up to this point are remembered as group #1 by the enclosing parentheses. After this any number of characters is matched out to the end of the line. At replace time, the contents of group #1 remembered before are substituted for the entire line.

    There are a lot of ways to do this with regular expressions. This is just one way.



  • Hello, @Rana-jawad,

    An equivalent regex to the Scott’s one would be :

    SEARCH (?-s)(?<=\|).*

    REPLACE Leave EMPTY !

    Notes :

    • The first part (?-s) is a modifier, which forces the regex engine to interpret the dot regex character as standing for any single standard character ( not End of Line characters )

    • The last part .* tries to match any range, even empty, of standard characters

    • The middle part (?<=\|) is a positive look-behind, that is to say a condition which must be true for an overall match

    • This condition implies that the matched range of characters must be preceded by a literal vertical bar ( | ), which have to be escaped with an \, because the | character is the regex symbol for alternation

    • And, due to the empty replacement box, this range of characters, located after the | character, is simply deleted


    However, just note that, with my regex or Scott regex, if some of your lines contains several | characters, any text after the first | character, will be deleted. For instance, the text, below :

    string1 | string2 | string3
    string4001 | string2668 | string1234
    string201 | string202 | string203
    

    would be changed into this one :

    string1 |
    string4001 |
    string201 |
    

    Best Regards,

    guy038


Log in to reply