regex

How do I bubble up an error from the closure passed to regex::Regex::replace?

那年仲夏 提交于 2021-02-17 06:42:06
问题 I have a function which performs string replacement in-place via regex::Regex::replace via a closure which performs some operations on the Captures : pub fn solve_dice_expression(expression: String) -> Result<i64, Box<dyn Error>> { lazy_static! { static ref PATTERN: Regex = Regex::new(r"(\d+)d(\d+)").expect("Problem compiling regex"); } // For every match on the Dice expression regex, roll it in-place. let rolled_expression = PATTERN.replace(&expression, |caps: &Captures| { let diceroll_str =

How do I bubble up an error from the closure passed to regex::Regex::replace?

大城市里の小女人 提交于 2021-02-17 06:42:02
问题 I have a function which performs string replacement in-place via regex::Regex::replace via a closure which performs some operations on the Captures : pub fn solve_dice_expression(expression: String) -> Result<i64, Box<dyn Error>> { lazy_static! { static ref PATTERN: Regex = Regex::new(r"(\d+)d(\d+)").expect("Problem compiling regex"); } // For every match on the Dice expression regex, roll it in-place. let rolled_expression = PATTERN.replace(&expression, |caps: &Captures| { let diceroll_str =

Postgresql regexp_matches syntax not working as expected

邮差的信 提交于 2021-02-17 06:40:34
问题 I use the Postgres regexp_matches function to extract numbers. The regular expression I use is 4([\s\-\/\.]*?0){3}([\s\-\/\.]*?[12]){1}([\s\-\/\.]*?\d){4} If I use a tool like https://regexr.com/ to verify if it's working and I apply the following test set 4-0001-1234 5-2342-2344 499999999 4-0001-1234 4.0001.12344 4-0-0-0-1-1234 I get the expected extraction result: 4-0001-1234 4-0001-1234 4.0001.1234 4-0-0-0-1-1234 However, if I use the same expression in Postgresql, it does work well:

Postgresql regexp_matches syntax not working as expected

本小妞迷上赌 提交于 2021-02-17 06:39:08
问题 I use the Postgres regexp_matches function to extract numbers. The regular expression I use is 4([\s\-\/\.]*?0){3}([\s\-\/\.]*?[12]){1}([\s\-\/\.]*?\d){4} If I use a tool like https://regexr.com/ to verify if it's working and I apply the following test set 4-0001-1234 5-2342-2344 499999999 4-0001-1234 4.0001.12344 4-0-0-0-1-1234 I get the expected extraction result: 4-0001-1234 4-0001-1234 4.0001.1234 4-0-0-0-1-1234 However, if I use the same expression in Postgresql, it does work well:

How to tell a RegEx to be greedy on an 'Or' Expression

北城以北 提交于 2021-02-17 06:23:12
问题 Text: [A]I'm an example text [] But I want to be included [[]] [A]I'm another text without a second part [] Regex: \[A\][\s\S]*?(?:(?=\[\])|(?=\[\[\]\])) Using the above regex, it's not possible to capture the second part of the first text. Demo Is there a way to tell the regex to be greedy on the 'or'-part? I want to capture the biggest group possible. Edit 1: Original Attempt: Demo Edit 2: What I want to achive: In our company, we're using a webservice to report our workingtime. I want to

Python: Error:TypeError: findall() missing 1 required positional argument: 'string'

穿精又带淫゛_ 提交于 2021-02-17 06:11:23
问题 I am trying to scrub a text document with specific parameters. Have tried different iterations of the x=... line but the program isn't able to read the all line. import re #import csv text = open(r'C:\Users\Vincent\Documents\python\theSortingHat\100000DirtyNames.txt') #open text file for line in text: #iterate through every line #return list of names in that line x = re.findall ('^([a-zA-Z]-?$') #if an actual name is found if x != 0: print(x) I receive: Error:TypeError: findall() missing 1

Python: Error:TypeError: findall() missing 1 required positional argument: 'string'

家住魔仙堡 提交于 2021-02-17 06:10:59
问题 I am trying to scrub a text document with specific parameters. Have tried different iterations of the x=... line but the program isn't able to read the all line. import re #import csv text = open(r'C:\Users\Vincent\Documents\python\theSortingHat\100000DirtyNames.txt') #open text file for line in text: #iterate through every line #return list of names in that line x = re.findall ('^([a-zA-Z]-?$') #if an actual name is found if x != 0: print(x) I receive: Error:TypeError: findall() missing 1

How does one parse best each item of an ingredient list and does create a new object based on each parsing result?

耗尽温柔 提交于 2021-02-17 06:05:21
问题 I have this list of ingredients I am trying to make a regex to look for 1 cup , or 1 tsp or 1 tablespoon and so on..... I have made this regex but It doesn't work as well. I am trying separate ingredients from the measurements. So with this string 1 Chopped Tomato it should take out the 1 as amount and output this: const output = [ { val: "Chopped Tomato", amount: "1", }, And with this string below it should be able to take out ½ tsp from ½ tsp fine salt and output this: const output = [ {

gsub with “|” character in R

谁说胖子不能爱 提交于 2021-02-17 05:45:31
问题 I have a data frame with strings under a variable with the | character. What I want is to remove anything downstream of the | character. For example, considering the string heat-shock protein hsp70, putative | location=Ld28_v01s1:1091329-1093293(-) | length=654 | sequence_SO=chromosome | SO=protein_coding I wish to have only: heat-shock protein hsp70, putative Do I need any escape character for the | character? If I do: a <- c("foo_5", "bar_7") gsub("*_.", "", a) I get: [1] "foo" "bar" i.e. I

python re.findall() with substring in alternations

那年仲夏 提交于 2021-02-17 05:45:19
问题 If I have a substring (or 'subpattern') of another string or pattern in a regex alternation, like so: r'abcd|bc' What is the expected behaviour of re.compile(r'abcd|bc').findall('abcd bcd bc ab') ? Trying it out, I get (as expected) ['abcd', 'bc', 'bc'] so I thought re.compile(r'bc|abcd').findall('abcd bcd bc ab') might yield ['bc', 'bc', 'bc'] but instead it again returns ['abcd', 'bc', 'bc'] Can someone explain this? I was under the impression that findall would greedily return matches but