Hot answers tagged

18

You should take a look at the possibility to name groups in regex patterns; your match pattern can then be a oneliner: const string pattern = @"^(?<name>\w+)\[(?<index>\d+)\]$"; Match match = Regex.Match(command[2], pattern); if (match.Success) { string featureName = match.Groups["name"].Value; int index = int.Parse(match....


12

One possible place you can improve performance is here: const string pattern = @"\[(.*?)\]"; var query = command[2]; var matches = Regex.Matches(query, pattern); //Gets anything inside the brackets index = Convert.ToInt32(matches[0].Groups[1].Value); //should be an int featureName = command[2].Substring(0, command[2].IndexOf('[')).ToUpper(); You're ...


7

Why would you want to use Regular Expressions anyway if the base format of the string is always the same? Name[Number] seems like an easy pattern. Just iterate through the characters one by one and store all characters in the first string until you reach the first bracket. Then store the numbers in the second string (until you reach the closing bracket). ...


4

Hash all string constants I'm presuming you only have a limited number of keywords ("move", "vertex" etc.). Hash all of those with something fast - CRC32 is perfectly adequate. Split the string into space-separated tokens, as usual, and calculate the hash of the first and second tokens. Then you just need to compare the calculated hash with the hashes of ...


4

Instead of throwing away that list of GovernmentCsvRecord objects after converting it to a StringCsv list, you could keep it around so you don't have to read the csv file again if the IsNumericFile check fails. It looks like you can validate the list of GovernmentCsvRecord objects directly instead of first converting them to StringCsv objects. However, with ...


3

Here's one: number_regex = re.compile( r'^[-+]?(?:(?:(?:[1-9](?:_?\d)*|0+(_?0)*)|(?:0[bB](?:_?[01])+)' r'|(?:0[oO](?:_?[0-7])+)|(?:0[xX](?:_?[0-9a-fA-F])+))' r'|(?:(?:(?:\d(?:_?\d)*)?(?:\.(?:\d(?:_?\d)*))|(?:\d(?:_?\d)*)\.)' r'|(?:(?:(?:\d(?:_?\d)*)|(?:(?:\d(?:_?\d)*)?(?:\.(?:\d(?:_?\d)*))' r'|(?:\d(?:_?\d)*)\.))(?:[eE][-+]?(?:\d(?:_?\d)*...


3

Your expression looks just fine, maybe we would slightly modify that to: ^[+-]?((\d+(\.\d+)?)|(\.\d+))$ for failing these samples, 3., 4., for instance, just in case maybe such samples might be undesired. Other than that, you have some capturing groups that I'm guessing you'd like to keep those. Test the capturing groups with re.finditer import re ...


3

The first obvious optimization would be to not call Regex.Matches(string, string) which compiles the regular expression every single time. Compiling the regex is an expensive operation. Instead, create a Regex (which compiles the expression exactly once) and keep it around during your several thousand invocations. That should by itself do the trick (because,...


2

Well, your regex solution is obviously badly broken - it will fail if the attributes are in a different order, if they are separated by newlines, if they are delimited by single quotes, etc etc. If you try to replace it with a more correct regex (it will never be 100% correct of course) then you are quite likely to lose some of this speed - perhaps ...


2

On my windows 10 laptop, I ran your program on a dummy log file under the python profiler using the command line: python -m cProfile -s cumulative loggrep.py "speech" \data\test.log The dummy log file has about 4.3 Mbytes of text spread across about 100k lines comprising 32772 log entries. The search pattern was "speech", which occured 444 times. ...


1

Pre-compilation In theory, you're going to want to call this function more than once. That means that you only want to pay the cost of regex compilation once, and you should move your re.compile calls out of the function, setting your regex variables in the module's global scope. Type-hinting s is s: str, and char is (I think) also char: str. 'Removed ...


Only top voted, non community-wiki answers of a minimum length are eligible