recentpopularlog in

kme : regex   82

« earlier  
perlfaq4 - Data Manipulation - metacpan.org | https://metacpan.org/
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" }
</code>

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 - metacpan.org | https://metacpan.org/
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
GitHub - tomnomnom/gf: A wrapper around grep, to help you grep for things
gf - A wrapper around grep to avoid typing common patterns.

What? Why?

I use grep a lot. When auditing code bases, looking at the output of meg, or just generally dealing with large amounts of data. I often end up using fairly complex patterns like this one:

▶ grep -HnrE '(\$_(POST|GET|COOKIE|REQUEST|SERVER|FILES)|php://(input|stdin))' *

It's really easy to mess up when typing all of that, and it can be hard to know if you haven't got any results because there are non to find, or because you screwed up writing the pattern or chose the wrong flags.

I wrote gf to give names to the pattern and flag combinations I use all the time. So the above command becomes simply:

▶ gf php-sources
go  cli  commandline  searching  textprocessing  patternmatching  regex  grep  alternativeto  ack  ag 
july 2019 by kme
Javascript regex multiline flag doesn't work - Stack Overflow | https://stackoverflow.com/

According to MDN, [^] also works to match any character, including newlines, in JavaScript.
javascript  regex  regexp  patternmatching  webdevel  solution 
october 2017 by kme
String.prototype.replace() - JavaScript | MDN | https://developer.mozilla.org/

Using an inline function that modifies the matched characters

In this example, all occurrences of capital letters in the string are converted to lower case, and a hyphen is inserted just before the match location. The important thing here is that additional operations are needed on the matched item before it is given back as a replacement.

The replacement function accepts the matched snippet as its parameter, and uses it to transform the case and concatenate the hyphen before returning.

<code class="language-javascript">
function styleHyphenFormat(propertyName) {
function upperToHyphenLower(match, offset, string) {
return (offset ? '-' : '') + match.toLowerCase();
}
return propertyName.replace(/[A-Z]/g, upperToHyphenLower);
}
</code>
javascript  regex  camelcase  kebabcase  solution  webdevel 
october 2017 by kme
html - RegEx match open tags except XHTML self-contained tags - Stack Overflow
I think the flaw here is that HTML is a Chomsky Type 2 grammar (context free grammar) and RegEx is a Chomsky Type 3 grammar (regular grammar). Since a Type 2 grammar is fundamentally more complex than a Type 3 grammar (see the Chomsky hierarchy), it is mathematically impossible to parse XML with RegEx.

But many will try, some will even claim success - but until others find the fault and totally mess you up.
textprocessing  webdevel  xml  parser  parsing  regex  funny  html  zalgowillcomeforyou 
june 2017 by kme
xpath - Pattern Matching with XSLT - Stack Overflow


Regular Expressions are supported only in XSLT 2.x/XPath 2.x.

As at this date, no publicly available browser supports XSLT 2.x/XPath 2.x.

In your concrete case you can use:

starts-with('awesome','awe')

other useful XPath 1.0 functions are:

contains()

substring()

substring-before()

substring-after()

normalize-space()

translate()

string-length()

scraping  screenscraping  webscraping  xpath  xslt  xml  html  patternmatching  regex  solution 
may 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: http://mywiki.wooledge.org/BashFAQ/031
bash  regex  regexp  patternmatching  newbie  dammitbrain 
december 2016 by kme
Right way to escape backslash [ ] in PHP regex? - Stack Overflow
Recommendation:
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
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
regex - How can I make my match non greedy in vim? - Stack Overflow
Instead of .* use .\{-}.

%s/style=".\{-}"//g

Also, see :help non-greedy
vim  regex  solution 
june 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.: https://www.emacswiki.org/emacs/RegularExpressionHelp)

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

See also: https://www.gnu.org/software/emacs/manual/html_node/emacs/Regexps.html#Regexps
regex  regexp  emacs  reference  coreutils  gnu  sysadmin  shell  regularexpression  syntax  essential  find 
december 2014 by kme
4.10.5.3 Common input element attributes — HTML Standard
This implies that the regular expression language used for this attribute is the same as that used in JavaScript, except that the pattern attribute is matched against the entire value, not just any subset (somewhat as if it implied a ^(?: at the start of the pattern and a )$ at the end).
html5  forms  validation  regex  webdevel 
june 2014 by kme
Non-Greedy Regular Expression Matching - Vincent Liu
For vim, the syntax is slightly more convoluted, so it's important to put a note here for my own reminder:


* (0 or more) greedy matching
\+ (1 or more) greedy matching
\{-} (0 or more) non-greedy matching
\{-n,} (at least n) non-greedy matching
patternmatching  vim  regex  solution 
december 2013 by kme
« earlier      
per page:    204080120160

Copy this bookmark:





to read