regex

Regular expression to extract the first and last number in a string

自作多情 提交于 2021-02-05 08:45:14
问题 Using these expressions separately, I can extract weight and price from this string, how can I get this to work in a single combined expression? String: 0.280 Price $ 2.49 Expressions (?<weight>\.?\d+(\x2E\d+)) (?<price>\d+\.\d+$) 回答1: (?<weight>\.?\d+(?:\x2E\d+)).*?(?<price>\d+\.\d+$) Try this.See demo. http://regex101.com/r/zR2tR4/21 来源: https://stackoverflow.com/questions/26021357/regular-expression-to-extract-the-first-and-last-number-in-a-string

Regex to read between multiline tags?

萝らか妹 提交于 2021-02-05 08:39:23
问题 I have text in this form : < text > some text efdg some text abcd < /text > I am writing a regex to extract : some text efdg some text abcd Since it is multiline I am using < text > \n+ (^+?) \n+ < text > , however It is not working. How can this be done? I tried using r'^.*?' but doesn't seem to be working. Code : Input file is : < doc > < id1 > 123 < /id1 > < text > abc def < /text > < /doc > < doc > < id1 > 1234 < /id1 > < text > abcdd defdd < /text > < /doc > for line in f.read().split('<

m regex pattern modifiers

女生的网名这么多〃 提交于 2021-02-05 08:38:32
问题 $subject = "SIverygood \n SIverygood\n"; $pattern = '/^SI/m'; preg_match_all($pattern, $subject, $matches2,PREG_OFFSET_CAPTURE); var_dump($matches2); I am unable to understand m and s modifier. I want to get SI at the beginning of every newline. I do it for practise purpose to understand the m and s modifier. The above example just return first SI . But i want both. 回答1: The m is a multi-line modifier which will makes the start anchor matches the start of each line instead of whole of your

m regex pattern modifiers

爷,独闯天下 提交于 2021-02-05 08:38:06
问题 $subject = "SIverygood \n SIverygood\n"; $pattern = '/^SI/m'; preg_match_all($pattern, $subject, $matches2,PREG_OFFSET_CAPTURE); var_dump($matches2); I am unable to understand m and s modifier. I want to get SI at the beginning of every newline. I do it for practise purpose to understand the m and s modifier. The above example just return first SI . But i want both. 回答1: The m is a multi-line modifier which will makes the start anchor matches the start of each line instead of whole of your

Javascript regex remove all line breaks after comma

非 Y 不嫁゛ 提交于 2021-02-05 08:34:12
问题 I need a simple regex that will remove all line breaks after comma. right know what I'm doing is removing all \n/g what do I need to add in order to remove only after comma? thanks. 回答1: Use replace(/,\n/g,"") For example "\n\n,\n".replace(/,\n+/g,",") 回答2: That's really simple. Try this: /,\n+/g Online Demo Full Code: var str = "first line,\nsecond line,\nalso, it is third line,\nand fourth line"; str = str.replace(/,\n+/g, ','); 来源: https://stackoverflow.com/questions/36154113/javascript

Regex match numbers not followed by a hyphen

元气小坏坏 提交于 2021-02-05 08:31:36
问题 I've worked with regexes for years and never had this much trouble working out a regex. I am using PHP 7.2.2's preg_match() to return an array of matching numbers for parsing, hence the parens in the regex. I am trying to match one or more numbers followed by an "x" followed by one or more numbers where the entire string is not followed by a hyphen. When $input is "18x18" or "18x18-", the matches are 18 and 1. When $input is "8x8" there are no matches. I seem to be doing something

Regex match numbers not followed by a hyphen

瘦欲@ 提交于 2021-02-05 08:31:24
问题 I've worked with regexes for years and never had this much trouble working out a regex. I am using PHP 7.2.2's preg_match() to return an array of matching numbers for parsing, hence the parens in the regex. I am trying to match one or more numbers followed by an "x" followed by one or more numbers where the entire string is not followed by a hyphen. When $input is "18x18" or "18x18-", the matches are 18 and 1. When $input is "8x8" there are no matches. I seem to be doing something

Using Lookahead and Lookbehind in regex to ignore a word anywhere between a BBCode

折月煮酒 提交于 2021-02-05 08:25:07
问题 I'm looking to expand on a particular code here: /(?<![@#]|(\[img\]))\b(".str_replace(" ", "[\-_ ]", $key).")(?!\[\/img\])\b/i Currently, it detects whether @ or # is directly behind the $key in question (which is fine), OR whether [img] or [/img] is directly before/after the $key (a problem). I want to add a wildcard so that the $key ANYWHERE in between [img] and [/img] will not be replaced, while still keeping the fact that @ or # must still be directly behind the $key . I am aware that

How to add dot to regex

江枫思渺然 提交于 2021-02-05 08:23:08
问题 @Pattern(regexp="^\\w{8,}") private String username; This pattern can only consist of numbers, letters and the underscore character. How can I add the dot (.) to the pattern Thanks 回答1: Try this, it might be easier to understand if you explicitly define all valid characters: @Pattern(regexp="^[A-Za-z0-9_.]{8,}") private String username; 回答2: You need to add end of the line anchor also. @Pattern(regexp="^[.\\w]{8,}$") 来源: https://stackoverflow.com/questions/29515276/how-to-add-dot-to-regex

Reflex to Delete all text except between two lines

雨燕双飞 提交于 2021-02-05 08:21:10
问题 I am trying to extract the content between two lines of string. Here is how the text looks Hhhshhajsjsjsj Hshhejjsjsmk Hahjajajajajja Message-ID: b123467 abc def Kjhshjsjs Received: Hdjjddjdjdjdjd I need to keep the text between ‘Message-ID’ and ‘Received:’ I tried ‘@“(?:\G(?!\A)[X-Message-ID:)\r?\n(.*)(?>\r?\nReceived:(?=S\r?$))?’ I have got an error ‘can’t find the text’ 回答1: Your problem would be straightforward if you only had a single occurrence of this text, and you only needed to