Extract some part of text separated by delimiter using regex

You can use this regex

[^;]+(?=;[^;]*$)

[^;] matches any character except ;

+ is a quantifier that matches preceding char or group 1 to many times

* is a quantifier that matches preceding char or group 0 to many times

$ is the end of the string

(?=pattern) is a lookahead which checks if a particular pattern occures ahead


/^(?:[^;]+;){3}([^;]+)/ will grab the 4th group between semicolons.

Although as stated in my comment, you should just split the string by semicolon and grab the 4th element of the split...that's the whole point of a delimited file, is you don't need complex pattern matching.

Example implementation in perl using your input example:

open(my $IN, "<input.txt") or die $!;

while(<$IN>){
    (my $desc) = $_ =~ /^(?:[^;]+;){3}([^;]+)/;
    print "'$desc'\n";
}
close $IN;

yields:

'Working as a Professor in University'
'He is a Software enginner at MNC'
'Working as a mechanical enginner'

Tags:

Regex

Aql