How to pad left a number with a specific amount of zeroes

C# has the built-in PadLeft() function for this: someString = someString.PadLeft(8, ‘0’); And here’s an article on it at Microsoft Learn. To use a regular expression, do something like this: string someText = “asd 123 rete”; someText = Regex.Replace(someText, @”\d+”, n => n.Value.PadLeft(8, ‘0’));

Replace with regex in Golang

You can use capturing groups with alternations matching either string boundaries or a character not _ (still using a word boundary): var re = regexp.MustCompile(`(^|[^_])\bproducts\b([^_]|$)`) s := re.ReplaceAllString(sample, `$1.$2`) Here is the Go demo and a regex demo. Notes on the pattern: (^|[^_]) – match string start (^) or a character other than _ \bproducts\b … Read more

What can I do when a regular expression pattern doesn’t match anywhere in a string?

Oh Yes You Can Use Regexes to Parse HTML! For the task you are attempting, regexes are perfectly fine! It is true that most people underestimate the difficulty of parsing HTML with regular expressions and therefore do so poorly. But this is not some fundamental flaw related to computational theory. That silliness is parroted a … Read more

JavaScript: String.indexOf(…) allowing regular-expressions?

Instances of the String constructor have a .search() method which accepts a RegExp and returns the index of the first match. To start the search from a particular position (faking the second parameter of .indexOf()) you can slice off the first i characters: str.slice(i).search(/re/) But this will get the index in the shorter string (after … Read more

Are there any good / interesting analogs to regular expressions in 2d?

Not being a regex expert, but finding the problem interesting, I looked around and found this interesting blog entry. Especially the syntax used there for defining the 2D regex looks appealing. The paper linked there might tell you more than me. Update from comment: Here is the link to the primary author’s page where you … Read more

Difference between regex_match and regex_search?

Assuming that C++ and Boost Regex have a similar structure and functionality, the difference between regex_match and regex_search is explained here: The regex_match() algorithm will only report success if the regex matches the whole input, from beginning to end. If the regex matches only a part of the input, regex_match() will return false. If you … Read more

Regex and unicode

Use a subrange of [\u0000-\uFFFF] for what you want. You can also use the re.UNICODE compile flag. The docs say that if UNICODE is set, \w will match the characters [0-9_] plus whatever is classified as alphanumeric in the Unicode character properties database. See also http://coding.derkeiler.com/Archive/Python/comp.lang.python/2004-05/2560.html.

Regex difference: (\w+)? and (\w*)

(\w+)? and (\w*) both match the same (0..+inf word characters) However, there is a slight difference: In the first case, if this part of the regex matches “”, the capturing group is absent. In the second case, it is empty. In some languages, the former manifests as a null while the latter should always be … Read more

What does this regexp mean – “\p{Lu}”?

These are considered Unicode properties. The Unicode property \p{L} — shorthand for \p{Letter} will match any kind of letter from any language. Therefore, \p{Lu} will match an uppercase letter that has a lowercase variant. And, the opposite \p{Ll} will match a lowercase letter that has an uppercase variant. Concisely, this would match any lowercase/uppercase that … Read more