How to replace all linebreaks in the start of string c# - c#

I've seen a lot of questions on how to replace all linebreaks.
However I have a scenario where I would like to replace all linebreaks in the start of the string only, there can be none, one or multiple and there is no way to know beforehand how many linebreaks it will be.
Question:
How do I remove all linebreaks that may occour before the string?
any help or input appreciated, thanks!

If you want to remove all leading occurrences of a set of characters, use String.TrimStart. Its prototype is:
public string TrimStart (params char[] trimChars);
In trimChars you put \n, in your case.

Related

Text files - how to programmatically mimic opening in Wordpad and overwriting as plain text [duplicate]

How can I replace lone instances of \n with \r\n (LF alone with CRLF) using a regular expression in C#?
I know to do it using plan String.Replace, like:
myStr.Replace("\n", "\r\n");
myStr.Replace("\r\r\n", "\r\n");
However, this is inelegant, and would destroy any "\r+\r\n" already in the text (although they are not likely to exist).
It might be faster if you use this.
(?<!\r)\n
It basically looks for any \n that is not preceded by a \r. This would most likely be faster, because in the other case, almost every letter matches [^\r], so it would capture that, and then look for the \n after that. In the example I gave, it would only stop when it found a \n, and them look before that to see if it found \r
Will this do?
[^\r]\n
Basically it matches a '\n' that is preceded with a character that is not '\r'.
If you want it to detect lines that start with just a single '\n' as well, then try
([^\r]|$)\n
Which says that it should match a '\n' but only those that is the first character of a line or those that are not preceded with '\r'
There might be special cases to check since you're messing with the definition of lines itself the '$' might not work too well. But I think you should get the idea.
EDIT: credit #Kibbee Using look-ahead s is clearly better since it won't capture the matched preceding character and should help with any edge cases as well. So here's a better regex + the code becomes:
myStr = Regex.Replace(myStr, "(?<!\r)\n", "\r\n");
I was trying to do the code below to a string and it was not working.
myStr.Replace("(?<!\r)\n", "\r\n")
I used Regex.Replace and it worked
Regex.Replace( oldValue, "(?<!\r)\n", "\r\n")
I guess that "myStr" is an object of type String, in that case, this is not regex.
\r and \n are the equivalents for CR and LF.
My best guess is that if you know that you have an \n for EACH line, no matter what, then you first should strip out every \r. Then replace all \n with \r\n.
The answer chakrit gives would also go, but then you need to use regex, but since you don't say what "myStr" is...
Edit:looking at the other examples tells me one thing.. why do the difficult things, when you can do it easy?, Because there is regex, is not the same as "must use" :D
Edit2: A tool is very valuable when fiddling with regex, xpath, and whatnot that gives you strange results, may I point you to: http://www.regexbuddy.com/
myStr.Replace("([^\r])\n", "$1\r\n");
$ may need to be a \
Try this: Replace(Char.ConvertFromUtf32(13), Char.ConvertFromUtf32(10) + Char.ConvertFromUtf32(13))
If I know the line endings must be one of CRLF or LF, something that works for me is
myStr.Replace("\r?\n", "\r\n");
This essentially does the same neslekkiM's answer except it performs only one replace operation on the string rather than two. This is also compatible with Regex engines that don't support negative lookbehinds or backreferences.

How to escape a delimiter by doubling the delimiter in a regex

I need to split a string on a delimiter, but not where the delimiter is doubled.
For instance "\m55.\m207|DEFAULT||DEFAULT|55||207" once split should result in
\m55.\m207
DEFAULT||DEFAULT
55||207
I'm trying to do this with a regex. If it makes a difference, I'm using C# System.Text.RegularExpression.Regex.
So far I have "[^|]\|[^|]" but that doesn't handle where an escaped delimiter is next to the delimter. IE |||
I'm sure there is a solution on the net, but I've tried searching with multiple different terms and couldn't find the right combination of terms to find it.
How do I escape the delimiter by doubling it in a regex? Or if there is a simpler solution what is it?
EDIT
Here is a more complicated example:
Input: "\m55.\m207|DEFAULT||DEFAULT|||55||207"
Expected output:
"\m55.\m207"
"DEFAULT||DEFAULT||"
"55||207"
Because your demo is so simple,and you just want to split with single |,so I can use \b here
string txt = #"\m55.\m207|DEFAULT||DEFAULT|55||207";
string patten = #"\b\|\b";
foreach (var str in Regex.Split(txt, patten))
{
Console.WriteLine(str);
}
(?<=[^|](?:\|{2})+)\|(?!\|)|(?<!\|)\|(?!\|)
You need to use lookarounds to make sure split happens on only one |.
See Demo

Get string between 2 characters Regex

I am new to regex and it really confuses me. What I am trying to accomplish is finding the string between 2 specified characters where the string should contain another specified character within it.
String Example: 'Help--Me'
In this case I would be looking for the string Help Me that's between the two apostrophes and contains -- in it.
The Regex I have currently is #"(?<=\')(--.*?)(?=\')"
This seems to only work if the -- is at the beginning of the string
Example: '--HelpMe'
Thanks in advance
You're very close—nice attempt. You need another wildcard string at the beginning:
#"(?<=\')(.*?--.*?)(?=\')"
This way, it'll look for a string of any characters following the ' (the minimum string, by the way, due to the non-greedy quantifier, *?), a --, another string of any characters (again, the minimum string), and finally the closing '.

c#: how to quickly insert a character into string in front of any occurrences of special combinations of characters?

I have a string where I need to escape any occurrences of special combinations of characters. In other words, I need to stick a "\" in front any occurrence of any such combination. Most combinations are actually single characters (e.g a double quote or a backslash) but some are multi-character (e.g. "&&"). One approach is to create an array of strings with these combinations, loop over them and run a String.Replace(), with the backslash being checked the last to avoid recursive escaping. But is there a better (more elegant/quick/etc) way of doing it? Thx
Use your idea of Replace but using an StringBuilder instead (much better perfomance).
You can use Regex.Replace for this.
var input = #"abc'def&&aa\cc""ff";
var output = Regex.Replace(input, #"'|&&|""|\\", m => #"\" + m); // => "abc\'def\&&aa\\cc\"ff"
you can just take your entire string and run String.Replace() for each replacement type you want to do, As far as I know that is the quickest/most elegant way to do it. Thats why it is a built in method.

What's wrong with my Regex expression

I have a written a Regex to check whether the string consists of 9 digits or not as follows but this always returning me false even if I have my string as 123456789.
if (Regex.IsMatch(strSSN, " ^\\d{9}$"))
Can any one tell what's wrong and also if any better one provide me .. What i am achieving is the string should have only numeric data and the length should be =9
Spaces are significant in regular expressions (and of course, you can't match a space character before the start of the string). Remove it and everything should be fine:
if (Regex.IsMatch(strSSN, "^\\d{9}$"))
Also, you generally want to use verbatim strings for regexes in C#, so you don't have to double your backslashes, but this is just a convenience, not the reason for your problem:
if (Regex.IsMatch(strSSN, #"^\d{9}$"))
this should work
^\d{9}$
hope that helps
You have a whitespace between " and ^. It should be:
if (Regex.IsMatch(strSSN, "^\\d{9}$"))
could it be the space at the beginning of your pattern? That seems wierd, a space then a number anchored to the start of the line.
This should give you what you want:
if (Regex.IsMatch(strSSN, "^\\d{9}$"))

Categories