recentpopularlog in

kme : regexp   51

perlfaq4 - Data Manipulation - |
How do I determine whether a scalar is a number/whole/integer/float?

Assuming that you don't care about IEEE notations like "NaN" or "Infinity", you probably just want to use a regular expression (see also perlretut and perlre):
<code class="language-perl">
use 5.010;

if ( /\D/ )
{ say "\thas nondigits"; }
if ( /^\d+\z/ )
{ say "\tis a whole number"; }
if ( /^-?\d+\z/ )
{ say "\tis an integer"; }
if ( /^[+-]?\d+\z/ )
{ say "\tis a +/- integer"; }
if ( /^-?(?:\d+\.?|\.\d)\d*\z/ )
{ say "\tis a real number"; }
if ( /^[+-]?(?=\.?\d)\d*\.?\d*(?:e[+-]?\d+)?\z/i )
{ say "\tis a C float" }

There are also some commonly used modules for the task. Scalar::Util (distributed with 5.8) provides access to perl's internal function looks_like_number for determining whether a variable looks like a number. Data::Types exports functions that validate data types using both the above and other regular expressions. Thirdly, there is Regexp::Common which has regular expressions to match various types of numbers. Those three modules are available from the CPAN.
perl  validation  regex  regexp  solution 
july 2019 by kme
perlfaq6 - Regular Expressions - |
In fact, the question that I had is sort-of-answered in perlfaq4, which refers you to Regexp::Common, which is, unfortunately, not part of the standard library.
This section is surprisingly small because the rest of the FAQ is littered with answers involving regular expressions. For example, decoding a URL and checking whether something is a number can be handled with regular expressions, but those answers are found elsewhere in this document (in perlfaq9: "How do I decode or create those %-encodings on the web" and perlfaq4: "How do I determine whether a scalar is a number/whole/integer/float", to be precise).
perl  regex  regexp  faq  newbie  reference 
july 2019 by kme
EmacsWiki: Regular Expression Help |
GNU Emacs (and GNU find) regexps *do* support alternates, but you have to escape *both* the parens and the pipe symbol.
emacs  gnu  gnufind  regexp  syntax  solution 
february 2019 by kme
Javascript regex multiline flag doesn't work - Stack Overflow |

According to MDN, [^] also works to match any character, including newlines, in JavaScript.
javascript  regex  regexp  patternmatching  webdevel  solution 
october 2017 by kme
bash regex match string - Stack Overflow

To match regexes you need to use the =~ operator.

Try this:

[[ sed-4.2.2.tar.bz2 =~ tar.bz2$ ]] && echo matched

Alternatively, you can use wildcards (instead of regexes) with the == operator:

[[ sed-4.2.2.tar.bz2 == *tar.bz2 ]] && echo matched

If portability is not a concern, I recommend using [[ instead of [ or test as it is safer and more powerful. See What is the difference between test, [ and [[ ? for details.

See also:
bash  regex  regexp  patternmatching  newbie  dammitbrain 
december 2016 by kme
Right way to escape backslash [ ] in PHP regex? - Stack Overflow
Always use four backslashes '\\\\' in a regex pattern when seeking to match a backslash.
regex  pcre  php  regexp  annoyance  webdevel  solution 
january 2016 by kme
regex - How do I perform a Perl substitution on a string while keeping the original? - Stack Overflow
This is the idiom I've always used to get a modified copy of a string without changing the original:

(my $new = $original) =~ s/foo/bar/;

In perl 5.14.0 or later, you can use the new /r non-destructive substitution modifier:

my $new = $old =~ s/foo/bar/r;
perl  regexp  idiom  dammitbrain  solution 
october 2015 by kme
Non greedy regex matching in sed? - Stack Overflow
Neither basic nor extended Posix/GNU regex recognizes the non-greedy quantifier; you need a later regex. Fortunately, Perl regex for this context is pretty easy to get:

perl -pe 's|(http://.*?/).*|\1|'
perl  sed  regexp  regex  nongreedy  quantifier  essential  textprocessing  dammitbrain  solution 
july 2015 by kme
How to negate specific word in regex? - Stack Overflow
I was trying to create a DataTable filter regexp which *didn't* match something anywhere in the line. What I wasn't doing was including the ".*" WITHIN the negative lookahead expression.
regexp  javascript  fuckina  dammitbrain  solution  patternmatching 
april 2015 by kme
EmacsWiki: Regular Expression
Emacs REs *do* support alternatives with the pipe character, but both the pipe and the parens have to be escaped (ref.:

GNU 'find' uses this syntax with '-regex' and '-iregex' by default.

See also:
regex  regexp  emacs  reference  coreutils  gnu  sysadmin  shell  regularexpression  syntax  essential  find 
december 2014 by kme
regex - Trim trailing spaces before newlines in a single multi-line string in JavaScript - Stack Overflow
Solution to an issue I was having where JavaScript loaded a file obtained by XHR into a textarea, but inserted a mysterious trailing newline that wasn't in the original file.
dd the 'm' multi-line modifier.

replace(/\s+$/gm, "")

Or faster still...

replace(/\s\s*$/gm, "")
webdevel  javascript  solution  patternmatching  regexp 
october 2014 by kme
My Collection of PHP Performance Benchmarks
* Use strpos in most cases where you just need to tell if a string contains (or starts with) another string:
strpos($haystack, $needle) !== false
strpos($haystack, $needle) === 0
php  webdevel  optimization  performance  stringmanipulation  patternmatching  regexp  comparison 
october 2014 by kme
Iterate over each line in a string in PHP - Stack Overflow
preg_split the variable containing the text, and iterate over the returned array:

foreach(preg_split("/((\r?\n)|(\r\n?))/", $subject) as $line){
// do stuff with $line

If you need to handle newlines in diferent systems you can simply use the PHP predefined constant PHP_EOL ( and simply use explode to avoid the overhead of the regular expression engine.

$lines = explode(PHP_EOL, $subject);
php  multilinestrings  regexp  webdevel  devel  solution 
october 2014 by kme
Coding Horror: Regex use vs. Regex abuse
validating a phone number

"^\(*\d{3}\)*( |-)*\d{3}( |-)*\d{4}$"

getting the trailing folder from a path


extracting the userid from a domain\userid pair

regexp  programming  commentary 
january 2014 by kme

Copy this bookmark:

to read