Why does re.sub replace the entire pattern, not just a capturing group within it?

Because it’s supposed to replace the whole occurrence of the pattern: Return the string obtained by replacing the leftmost non-overlapping occurrences of the pattern in string by the replacement repl. If it were to replace only some subgroup, then complex regexes with several groups wouldn’t work. There are several possible solutions: Specify pattern in full: … Read more

Get index of each capture in a JavaScript regex

There is currently a proposal (stage 4) to implement this in native Javascript: RegExp Match Indices for ECMAScript ECMAScript RegExp Match Indices provide additional information about the start and end indices of captured substrings relative to the start of the input string. …We propose the adoption of an additional indices property on the array result … Read more

Scala capture group using regex

Here’s an example of how you can access group(1) of each match: val string = “one493two483three” val pattern = “””two(\d+)three”””.r pattern.findAllIn(string).matchData foreach { m => println(m.group(1)) } This prints “483” (as seen on ideone.com). The lookaround option Depending on the complexity of the pattern, you can also use lookarounds to only match the portion you … Read more

What does this Django regular expression mean? `?P`

In django, named capturing groups are passed to your view as keyword arguments. Unnamed capturing groups (just a parenthesis) are passed to your view as arguments. The ?P is a named capturing group, as opposed to an unnamed capturing group. http://docs.python.org/library/re.html (?P<name>…) Similar to regular parentheses, but the substring matched by the group is accessible … Read more

How to capture an arbitrary number of groups in JavaScript Regexp?

When you repeat a capturing group, in most flavors, only the last capture is kept; any previous capture is overwritten. In some flavor, e.g. .NET, you can get all intermediate captures, but this is not the case with Javascript. That is, in Javascript, if you have a pattern with N capturing groups, you can only … Read more

Vim Regex Capture Groups [bau -> byau : ceu -> cyeu]

One way to fix this is by ensuring the pattern is enclosed by escaped parentheses: :%s/\(\w\)\(\w\w\)/\1y\2/g Slightly shorter (and more magic-al) is to use \v, meaning that in the pattern after it all ASCII characters except ‘0’-‘9’, ‘a’-‘z’, ‘A’-‘Z’ and ‘_’ have a special meaning: :%s/\v(\w)(\w\w)/\1y\2/g See: :help \( :help \v

What is a non-capturing group in regular expressions?

Let me try to explain this with an example. Consider the following text: http://stackoverflow.com/ https://stackoverflow.com/questions/tagged/regex Now, if I apply the regex below over it… (https?|ftp)://([^/\r\n]+)(/[^\r\n]*)? … I would get the following result: Match “http://stackoverflow.com/” Group 1: “http” Group 2: “stackoverflow.com” Group 3: “https://stackoverflow.com/” Match “https://stackoverflow.com/questions/tagged/regex” Group 1: “https” Group 2: “stackoverflow.com” Group 3: “https://stackoverflow.com/questions/tagged/regex” But … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)