Regex to match only the first line?

that’s sounds more like a job for the filehandle buffer. You should be able to match the first line with: /^(.*)$/m (as always, this is PCRE syntax) the /m modifier makes ^ and $ match embedded newlines. Since there’s no /g modifier, it will just process the first occurrence, which is the first line, and … Read more

Javascript regex to validate IPv4 and IPv6 address, no hostnames

I had the exact same necessity, so I adapted the regex from Daniel’s great answer (which is the most accurate I was able to find) to NOT validate hostnames. Here it is: var expression = /((^\s*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\s*$)|(^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$))/; if (expression.test(valueToTest)) { // good IP } else { // bad IP } I forked Daniel’s original JSFiddle to … Read more

How to match numbers between X and Y with regexp?

According to Generate a Regular Expression to Match an Arbitrary Numeric Range, and after generating such a regex for your example at Regex_For_Range: \b0*(1[1-9][0-9]|[2-9][0-9]{2}|1[0-9]{3}|2[01][0-9]{2}|22[0-2][0-9]|223[0-4])\b would do the trick. The process would be (still following that Regex generator): First, break into equal length ranges: 110 – 999 1000 – 2234 Second, break into ranges that yield … Read more

sed : how to print all lines after line with a match?

To print all lines after, and including, the first match: $ echo -ne ‘apple\nbanana\ncherry\n’ | sed -ne ‘/banana/,$ p’ banana cherry To print all lines after, and NOT including, the first match: $ echo -ne ‘apple\nbanana\ncherry\n’ | sed -e ‘1,/banana/ d’ cherry Filtering lines when pattern matches between “text=” and “status=” can be done with … Read more

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

tech