I have this kind of text:
LINE\r\n 5\r\n11DA3\r\n330\r\n2\r\n100\r\nAcDbEntity\r\n
8\r\n0-FD\r\n 6\r\nHIDDEN\r\n100
Take a look at the text in bold. I would like to replace the text between 5\r\n and \r\n100. I tried this code:
result[line] = Regex.Replace(result[line], #"((?<=5\r\n)(\S+?)(?=\r\n100))", "0");
But it doesn't work. Is there something wrong with my code? I was sure the (\S+?) is the problem. Any way to solve it?
you can use the code:
string type_1 = "LINE\r\n 5\r\n11DA3\r\n330\r\n2\r\n100\r\nAcDbEntity\r\n 8\r\n0-FD\r\n 6\r\nHIDDEN\r\n100";
string output = Regex.Replace (
type_1,
"5\r\n(.*?)\r\n100",
"5\r\n0\r\n100",
RegexOptions.Singleline|RegexOptions.Compiled
);
Console.WriteLine (output);
it outputs:
LINE
5
0
100,1
AcDbEntity
8
0-FD
6
HIDDEN
100
It will change all encounters of text 5\r\n - ANYTHING HERE - \r\n100 to 5\r\n0\r\n100. If you want a more specific change please let me know.
If the removable Contents are Static you can use
s.Replace("11DA3\r\n330\r\n2" ,100);
Or even you can try with string.indexof
Related
I know the remove method is used like this:
string MyString = "Hello Beautiful Magic World";
Console.WriteLine(MyString.Remove(5,10));
// The example displays the following output:
// Hello Magic World
I want to keep the 5 characters at the end of the string ("World" in our example) constant and remove the previous 5 characters.
But I don't have the slightest idea how to do it. I tried like this: Unfortunately it did not.
MyString.Substring(0, MyString.Length - 10 - 5);
The following will find the word "Magic" and remove it:
string stringToRemove = myString.Remove(myString.Length - 11, 6);
I have a text file with over 12,000 lines. In that file I need to replace certain lines.
Some lines begin with a ;, some have random words, some start with space. However, I am only concerned with the two types of lines I describe below.
I have a line like
SET avariable:0 ;Comments
and I need to replace it to look like
set aDIFFvariable:0 :Integer // comments
The only CASE that is necessary is in the word Integer I needs to be capitalized.
I also have
String aSTRING(7) ;Comment
that needs to look like
STRING aSTRING(7) :array [0..7] of AnsiChar; // Comments
I need to keep all the spacing the same.
Here is what I have so far
static void Main(string[] args)
{
string text = File.ReadAllText("C:\\old.txt");
text = text.Replace("old text", "new text");
File.WriteAllText("C:\\new.txt", text);
}
I think I need to use REGEX, which I have tried to make for my first example:
\s\s[set]\s*{4}.*[:0]\s*[;].* <-- I now know this is invalid - please advise
I need help with properly setting up my program to find and replace those lines. Should I read one line at a time and if it matches then do something? I am confused really as to where to start.
BRIEF pseudo code of what I want to do
//open file
//step through file
//if line == [regex] then add/replace as needed
//else, go to next line
//if EOF, close file
Taking a stab at this separately because each line is so radically different that capturing both in the same expression will be a nightmare.
To match your first example and replace it:
String input = "SET avariable:0 ;Comments";
if (Regex.IsMatch(input, #"\s?(set)\s*(\w+):?(\d)\s+;?(.*)?"))
{
input = Regex.Replace(input, #"\s?(set)\s*(\w+):?(\d)\s+;?(.*)?", "$1 $2:$3 :Integer // $4";
}
Give that a shot (Play with it here: http://regex101.com/r/zY7hV2)
To match your second example and replace it:
String input = "String aSTRING(7) ;Comments";
if (Regex.IsMatch(input, #"\s?(string)\s*(\w+)\((\d)\)\s*;(.*)"))
{
input = Regex.Replace(input, #"\s?(string)\s*(\w+)\((\d)\)\s*;(.*)", "$1 $2($3) :array [0..$3] of AnsiChar; // $4";
}
And play around with this one here: http://regex101.com/r/jO5wP5
I have a mental block and can't seem to figure this out, sure its pretty easy 0_o
I have the following string: "5555S1"
String can contain any number of digits, followed by a Letter(A-Z), followed by numbers again.
How do I get the index of the Letter(S), so that I can substring so get everything following the Letter
Ie: 5555S1
Should return S1
Cheers
You could also check if the integer representation of the character is >= 65 && <=90.
Simple Python:
test = '5555Z187456764587368457638'
for i in range(0,len(test)):
if test[i].isalpha():
break
print test[i:]
Yields: Z187456764587368457638
Given that you didn't say what language your using I'm going to pick the one I want to answer in - c#
String.Index see http://msdn.microsoft.com/en-us/library/system.string.indexof.aspx for more
for good measure here it is in java string.indexOf
One way could be to loop through the string untill you find a letter.
while(! isAlpha(s[i])
i++;
or something should work.
This doesn't answer your question but it does solve your problem.
(Although you can use it to work out the index)
Your problem is a good candidate for Regular Expressions (regex)
Here is one I prepared earlier:
String code = "1234A0987";
//timeout optional but needed for security (so bad guys dont overload your server)
TimeSpan timeout = TimeSpan.FromMilliseconds(150);
//Magic here:
//Pattern == (Block of 1 or more numbers)(block of 1 or more not numbers)(Block of 1 or more numbers)
String regexPattern = #"^(?<firstNum>\d+)(?<notNumber>\D+)(?<SecondNum>\d+)?";
Regex r = new Regex(regexPattern, RegexOptions.None, timeout);
Match m = r.Match(code);
if (m.Success)//We got a match!
{
Console.WriteLine ("SecondNumber: {0}",r.Match(code).Result("${SecondNum}"));
Console.WriteLine("All data (formatted): {0}",r.Match(code).Result("${firstNum}-${notNumber}-${SecondNum}"));
Console.WriteLine("Offset length (not that you need it now): {0}", r.Match(code).Result("${firstNum}").Length);
}
Output:
SecondNumber: 0987
All data (formatted): 1234-A-0987
Offset length (not that you need it now): 4
Further info on this example here.
So there you go you can even work out what that index was.
Regex cheat sheet
I have a string like "lenovo", passing it to a simple method like this to convert it to RTF:
RichTextBox rtx = new RichTextBox();
rtx.Text = sText;
string s = rtx.Rtf;
return s;
the result is something like this:
"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0
Microsoft Sans Serif;}}\r\n\viewkind4\uc1\pard\f0\fs17
lenovo\par\r\n}\r\n"
it has a //par and then two \r\n at the end, this will become an issue later when I am getting this RTF and showing in a report, it will cause a line break :(
What is going wrong? and how can I fix it?
Thanks
Yes, annoying.
Annoying fix:
rtx.SelectAll();
string s = rtx.SelectedRtf;
I am lookind for a effective way that I can replace newlines with an auto-incrementing number.
eg.
this is line 1
this is line 2
this is line 3
this is line 4
to
1. this is line 1
2. this is line 2
3. this is line 3
4. this is line 4
Is looping through each line the only way? I guess thats the way I will implement it for now. Unless I find a better way here :) Just some pseudo code will do. But I am using C#
This is probably easier if you use a shell command, for example:
nl -ba -s'. ' -w1
Just some pseudo code will do.
int lineNo=1;
for(String str:listOfString){
System.out.println(lineNo + " : " + str);
lineNo++;
}
Note: code provided is written in java , You can get the basic idea from that
You can use LINQ:
string[] lines = ...
string newText = string.Concat(lines.Select(
(line, index) => string.Format("{0}. {1}", index, line)));