How to delete everything after second slash
Use an expression ^([^/]*/[^/]*/).*$
, and replace with $1
([^/]*/[^/]*/)
expression captures the part that you want to keep; .*$
captures the rest of the string to the end of line. Replacing with $1
removes the unwanted portion of the string.
Remove everything after a changing string
with cut:
cut -d\/ -f1,2,3 file
Regex for matching everything after second-to-last backslash
You can use
@"[^\\]+\\[^\\]+$"
See regex demo
The [^\\]+\\[^\\]+$
matches
[^\\]+
- 1 or more symbols other than\
\\
- a literal\
[^\\]+
- 1 or more symbols other than\
$
- end of string.
In C#, a more efficient way to match some substring at the end is using RegexOptions.RightToLeft
modifier. Use it to make this regex search more efficient.
C# demo:
var line = @"0001 Lorem ipsum dolor sit\011 tortor neque\ 0111\interdum magn";
var pattern = @"[^\\]+\\[^\\]+$";
var result = Regex.Match(line, pattern, RegexOptions.RightToLeft);
if (result.Success)
Console.WriteLine(result.Value); // => " 0111\interdum magn"
Just a comparison of the regex effeciency with and without RTL option at regexhero.net:
Regular Expression for getting everything after last slash
No, an ^
inside []
means negation.
[/]
stands for 'any character in set [/]'.
[^/]
stands for 'any character not in set [/]'.
Regular Expression, remove everything after last forward slash
Replace /[^/]*$
with an empty string
Regex for remove everything after | (with | )
The pipe, |
, is a special-character in regex (meaning "or") and you'll have to escape it with a \
.
Using your current regex:
\|.*$
I've tried this in Notepad++, as you've mentioned, and it appears to work well.
Remove everything after last backslash
You need lastIndexOf
and substr
...
var t = "\\some\\route\\here";
t = t.substr(0, t.lastIndexOf("\\"));
alert(t);
Also, you need to double up \
chars in strings as they are used for escaping special characters.
Update
Since this is regularly proving useful for others, here's a snippet example...
// the original stringvar t = "\\some\\route\\here";
// remove everything after the last backslashvar afterWith = t.substr(0, t.lastIndexOf("\\") + 1);
// remove everything after & including the last backslashvar afterWithout = t.substr(0, t.lastIndexOf("\\"));
// show the resultsconsole.log("before : " + t);console.log("after (with \\) : " + afterWith);console.log("after (without \\) : " + afterWithout);
Related Topics
Updating an Object from a List in C#
Client Specific Role Based Authentication
Convert Byte Array to Wav File
Angular Post Request Received in Net Core API as Null
Regular Expression for Anything But an Empty String
How to Use a Class from One C# Project With Another C# Project
How to Count Rows Within Entityframework Without Loading Contents
Regular Expression for Password Validation in C#
How to Read Request Body in an ASP.NET Core Webapi Controller
How to Return a File (Filecontentresult) in ASP.NET Webapi
How to Ignore First Two Columns of CSV File
How to Check If a Socket Is Connected/Disconnected in C#
Most Efficient Way to Compare Two Ienumerables (Or Lists) in Linq
How to Check for a Valid Base64 Encoded String
C# - Comparing Items Between 2 Lists
Deserialize Json in C# - How to Handle Null Return Values
How to Get Group of Records With Latest Date Using Linq to Entities