codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Why does this regex work in Rubular, but not in Python?


By : , Category : python

Can't tell for sure without seeing the code, but I'm guessing you forgot to use the multiline flag, re.MULTILINE.

Ruby is unique in that ^/$ always match start/end of line. In other common flavors they do not unless the m flag is used.

ReLated :

You need to use m modifier for multiline regexp, since there are in the string:

#                  ⇓
to_clean.gsub!( /…/m, …)
#                  ⇑

This will work.

Bash definitely does not understand perl-compatible regular expressions. I'd stick to bash idioms:

string="one/two one/three two/four five/six seven"
roots=$(sed 's//[^[:blank:]]*//g' <<< "$string" | tr ' ' '
' | sort -u)
echo "$roots"

or

roots=()                        # empty array
for word in $string             # no quotes to obtain word splitting
do
    roots+=( ${word%/*} )       # add to the array the bit before the last slash
done
printf "%s
" "${roots[@]}" | sort -u

or, with bash 4, use an associative array to mimic the behaviour of a set.

declare -A roots                # an associative array
for word in $string             # no quotes to obtain word splitting
do
    roots[${word%/*}]=1
done
printf "%s
" "${!roots[@]}"    # print out the hash keys

Because you use match, with returns one matching element. If you use scan instead, all should work properly:

string = "hello ..a.sdf asdf..,"
string.downcase.scan(/[w&&S]+/)
# => ["hello", "a", "sdf", "asdf"]

You need to use an extended regular expression for grep to work in this case. Add the -E flag. I get pretty much the same output for all three tools in that case:

ack

ag

grep

Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .