string-parsing

How do you get an unsigned long out of a string?

百般思念 提交于 2019-12-05 01:38:12
问题 What's the safest and best way to retrieve an unsigned long from a string in C++? I know of a number of possible methods. First, converting a signed long taken from atol. char *myStr; // Initalized to some value somehow. unsigned long n = ((unsigned)atol(myStr)); The obvious problem with this is, what happens when the value stored in myStr is larger than a signed long can contain? What does atol retrieve? The next possibility is to use strtoul. char *myStr; // Initalized to some value somehow

Regex with non-capturing group in C#

我的未来我决定 提交于 2019-12-05 00:35:26
I am using the following Regex JOINTS.*\s*(?:(\d*\s*\S*\s*\S*\s*\S*)\r\n\s*)* on the following type of data: JOINTS DISPL.-X DISPL.-Y ROTATION 1 0.000000E+00 0.975415E+01 0.616921E+01 2 0.000000E+00 0.000000E+00 0.000000E+00 The idea is to extract two groups, each containing a line (starting with the Joint Number, 1, 2, etc.) The C# code is as follows: string jointPattern = @"JOINTS.*\s*(?:(\d*\s*\S*\s*\S*\s*\S*)\r\n\s*)*"; MatchCollection mc = Regex.Matches(outFileSection, jointPattern ); foreach (Capture c in mc[0].Captures) { JointOutput j = new JointOutput(); string[] vals = c.Value.Split(

Remove blank lines in a text file

旧巷老猫 提交于 2019-12-05 00:08:28
问题 How can you remove blank lines from a text file in C#? 回答1: If file is small: var lines = File.ReadAllLines(fileName).Where(arg => !string.IsNullOrWhiteSpace(arg)); File.WriteAllLines(fileName, lines); If file is huge: var tempFileName = Path.GetTempFileName(); try { using (var streamReader = new StreamReader(inptuFileName)) using (var streamWriter = new StreamWriter(tempFileName)) { string line; while ((line = streamReader.ReadLine()) != null) { if (!string.IsNullOrWhiteSpace(line))

Parsing / Extracting Text from String in Rails?

白昼怎懂夜的黑 提交于 2019-12-04 19:53:45
I have a string in Rails, e.g. "This is a Twitter message. #books War & Peace by Leo Tolstoy. I love this book!", and I want to parse the text and extract only certain phrases, like "War & Peace by Leo Tolstoy". Is this a matter of using Regex and lifting the text between "#books" to "."? What if there's no structure to the message, like: "This is a Twitter message #books War & Peace by Leo Tolstoy I love this book!" or "This is a Twitter message. I love the book War & Peace by Leo Tolstoy #books" How can I reliably pull the phrase "War & Peace by Leo Tolstoy" without knowing the phrase ex

How to interpret numbers correctly (hex, oct, dec)

微笑、不失礼 提交于 2019-12-04 19:36:46
I'm trying to write a program that takes input of - hexadecimals, octals, and decimals -, stores them in integer variables, and outputs them along with their conversion to decimal form. For example: User inputs: 0x43, 0123, 65 Program outputs: 0x43 hexadecimal converts to 67 decimal 0123 octal converts to 83 decimal 65 decimal converts to 65 decimal So obviously I need a way to interpret the numbers, but I'm not sure how to go about doing it. I've tried various methods such as reading them into a function and converting them into a string, and vice versa (see here for code examples), but

Java source code parser

跟風遠走 提交于 2019-12-04 07:10:26
I need to programmatically extract method definition/implementation from Java source file, could you recommend any handy library that I can use? Thanks. Another option is Jflex and CUP . As per my usage experience, I liked the syntax more than ANTLR. However ANTLR scores more when it comes to IDE support and documentation.. ANTLR is a parser/generator that has grammars for Java (and other languages). It can produce an abstract syntax tree (AST) that you can navigate and manipulate as you wish. if you look for a simple parser, i'd avise JavaParser (a much heavier implementation is also included

Perl/regex to remove first 3 lines and last 3 lines of a string

谁说胖子不能爱 提交于 2019-12-04 05:56:56
I was looking to build a regex statement to always remove the first 3 lines of the string, and last 3 lines of the string (the middle part could be any n number of lines content). Any clean regex way to acheive this output? (i.e. always strip our first 3 lines and last 3 lines of the string - and preserve the middle part, which could be a variable # of lines) Thanks. e.g. Input String: " 1 2 3 <1...n # of lines content> 4 5 6 " To desired output string: "<1..n # of lines content>" The previously given solutions are really complex! All one needs is s/^(?:.*\n){1,3}//; s/(?:.*\n){1,3}\z//; If

Are there character collections for all international full stop punctuations?

风流意气都作罢 提交于 2019-12-04 05:04:09
I am trying to parse utf-8 strings into "bite sized" segments. For example, I would like to break down a text into "sentences". Is there a comprehensive collection of characters (or regex) that correspond to end of sentences in all languages? I'm looking for something that would capture the Latin period, exclamation and interrogation marks, the Chinese and Japanese full stop, etc. Something like the above but for the equivalent of a comma would be great too. I haven’t encountered any compilations of such information, and I would expect it to be a major effort to collect it. For some widely

Remove blank lines in a text file

喜欢而已 提交于 2019-12-03 16:58:08
How can you remove blank lines from a text file in C#? If file is small: var lines = File.ReadAllLines(fileName).Where(arg => !string.IsNullOrWhiteSpace(arg)); File.WriteAllLines(fileName, lines); If file is huge: var tempFileName = Path.GetTempFileName(); try { using (var streamReader = new StreamReader(inptuFileName)) using (var streamWriter = new StreamWriter(tempFileName)) { string line; while ((line = streamReader.ReadLine()) != null) { if (!string.IsNullOrWhiteSpace(line)) streamWriter.WriteLine(line); } } File.Copy(tempFileName, inptuFileName, true); } finally { File.Delete(tempFileName

How do you get an unsigned long out of a string?

ぃ、小莉子 提交于 2019-12-03 15:50:09
What's the safest and best way to retrieve an unsigned long from a string in C++? I know of a number of possible methods. First, converting a signed long taken from atol. char *myStr; // Initalized to some value somehow. unsigned long n = ((unsigned)atol(myStr)); The obvious problem with this is, what happens when the value stored in myStr is larger than a signed long can contain? What does atol retrieve? The next possibility is to use strtoul. char *myStr; // Initalized to some value somehow. unsigned long n = strtoul(myStr, 0, 10); However, this is a little over complicated for my needs. I'd