regex-group

How to loop through, match and replace?

丶灬走出姿态 提交于 2020-01-11 11:25:12
问题 I have multiple strings with same curly braces I want to replace them as dynamic if I get the count as 1 then need to replace the first occurrence, If count as 2 then replaces the second occurrence as so on until condition satisfies. <?php include_once("con.php"); $db = new Da(); $con = $db->con(); $String = "{{ONE}} {{TWO}} {{THREE}} {{FOUR}} {{FIVE}} {{SIX}}"; $Count = 1; if(preg_match_all("/\{\{[^{}]+\}\}/", $lclString, $matches)) { foreach ($matches[0] as $match) { $Count++; $Query =

How exactly does this recursive regex work?

℡╲_俬逩灬. 提交于 2019-12-30 09:29:23
问题 This is a followup to this question. Have a look at this pattern: (o(?1)?o) It matches any sequence of o with a length of 2 n , with n ≥ 1. It works, see regex101.com (word boundaries added for better demonstration). The question is: Why? In the following, the description of a string (match or not) will simply be a bolded number or a bolded term that describes the length, like 2 n . Broken down (with added whitespaces): ( o (?1)? o ) ( ) # Capture group 1 o o # Matches an o each at the start

C++11 regex: digit after capturing group in replacement string

∥☆過路亽.° 提交于 2019-12-30 08:08:48
问题 My regex_replace expression uses group $1 right before a '0' character in the replacement string like so: #include <iostream> #include <string> #include <regex> using namespace std; int main() { regex regex_a( "(.*)bar(.*)" ); cout << regex_replace( "foobar0x1", regex_a, "$10xNUM" ) << endl; cout << regex_replace( "foobar0x1", regex_a, "$1 0xNUM" ) << endl; } The output is: xNUM foo 0xNUM I'm trying to get output foo0xNUM without the middle whitespace. How do I guard the group name $1 from

Python Regex instantly replace groups

狂风中的少年 提交于 2019-12-27 12:20:49
问题 Is there any way to directly replace all groups using regex syntax? The normal way: re.match(r"(?:aaa)(_bbb)", string1).group(1) But I want to achieve something like this: re.match(r"(\d.*?)\s(\d.*?)", "(CALL_GROUP_1) (CALL_GROUP_2)") I want to build the new string instantaneously from the groups the Regex just captured. 回答1: Have a look at re.sub: result = re.sub(r"(\d.*?)\s(\d.*?)", r"\1 \2", string1) This is Python's regex substitution (replace) function. The replacement string can be

Java Regex Multiple Pattern Group Replace

我怕爱的太早我们不能终老 提交于 2019-12-25 03:55:10
问题 Say I have the following string String Context = "old1 old2 old3 old4 old5 old6" I wish to do a pattern: (old2).*(old4) so word 2 would be in $1 and word4 would be in $2 . Is there any functions or methods that i can replace the two words strings at the same time? Using only the group variable ( $1 and $2 ) ? So I could specify that $1 will become new2 and $2 will become new4 . I don't want to have to find the string old2 and old4 and replace it to new2 and new4 回答1: Only One Group Needed If

RegEx Return found Group IF String Does NOT start with ' - VSCode - Search/Replace

橙三吉。 提交于 2019-12-25 03:16:18
问题 So, I did a global undoable RegEx search and replace. I forgot to include the ' in the replace. Now I need to do a search on strings that match the below. It must not start with a ' and will have | translate at the end. These are Angular translation keys - they can be all over in the template file (HTML). They always begin with {{, have | translate, and end with }}. Now the kicker is they could have spacing or line break issues (less likely but a chance). So it could be {{_ _ textToKeepAdd'To

Extract number values enclosed inside curly brackets

天大地大妈咪最大 提交于 2019-12-24 07:29:18
问题 I want to extract some string data from a given file. File got structure such as: name, catg, {y:2006, v:1000, c:100, vt:1}, {y:2007, v:1000, c:100, vt:1},.. {..}.. . I want to extract next values: name; catg; numbers after y , v , c , vt labels; I used the next regexes: @"(?<name>\w+), (?<cat>\w+)" for extraction of the first two items; @"(?:\{y:(?<y>\d+), +v:(?<v>\d+), +c:(?<c>\d+), +vt:(?<vt>\d+)\}, ?)+" for extraction of other values enclosed in curly brackets. I concatenated those two

Replace content present in the nested brackets

*爱你&永不变心* 提交于 2019-12-24 04:35:15
问题 Input = ABCDEF ((3) abcdef),GHIJKLMN ((4)(5) Value),OPQRSTUVW((4(5)) Value (3)) Expected Output = ABCDEF,GHIJKLMN,OPQRSTUVW Tried so far Output = Input.replace(/ *\([^)]*\)*/g, ""); 回答1: Using a regex here probably won't work, or scale, because you expect nested parentheses in your input string. Regex works well when there is a known and fixed structure to the input. Instead, I would recommend that you approach this using a parser . In the code below, I iterate over the input string, one

python multiline regex to get optional group in parentheses

独自空忆成欢 提交于 2019-12-24 04:32:10
问题 I am struggling to write a regex to basically get 4 groups for both tables Table Name e.g. table_v1 Table columns in first () after table name Primary key values in () Optionally Value in () if CLUSTERING ORDER is there I tried this, mostly works except cannot get cluster order value . EDIT: HERE IS A FAILING DEMO re.compile("CREATE\s+TABLE\s+(?:[a-z][a-z0-9_]*).*?((?:[a-z][a-z0-9_"]*)).*?(\(.*?\)) WITH.*?(\(.*?\)).*?;").findall(string_below) Here is the String trying to run above regex on.

python multiline regex to get optional group in parentheses

余生长醉 提交于 2019-12-24 04:32:08
问题 I am struggling to write a regex to basically get 4 groups for both tables Table Name e.g. table_v1 Table columns in first () after table name Primary key values in () Optionally Value in () if CLUSTERING ORDER is there I tried this, mostly works except cannot get cluster order value . EDIT: HERE IS A FAILING DEMO re.compile("CREATE\s+TABLE\s+(?:[a-z][a-z0-9_]*).*?((?:[a-z][a-z0-9_"]*)).*?(\(.*?\)) WITH.*?(\(.*?\)).*?;").findall(string_below) Here is the String trying to run above regex on.