BurningBright

  • Home

  • Tags

  • Categories

  • Archives

  • Search

Python rename files

Posted on 2017-02-21 | Edited on 2020-06-18 | In python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import os
import os.path

# point to the target directory
dirpath = 'F:\\a\\'

# loop begin
for file in os.listdir(dirpath):
# if it's a file
if os.path.isfile(os.path.join(dirpath, file)) == True:
newname = '[BOMB.tv]' + file
#rename it
os.rename(os.path.join(dirpath, file),os.path.join(dirpath, newname))
print (file,'ok')

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import os
import sys

dirpath = 'F:\\a\\'

#three parameters: parent directory, all directory name, all file names
for parent,dirnames,filenames in os.walk(dirpath):
for filename in filenames:
# format output
old = "%s/%s" %(dirpath, filename)
new = "%s/%s" %(dirpath, "[BOMB.tv]" + filename)
os.rename(old, new)

# output directory info
for dirname in dirnames:
print ("parent is:" + parent)
print ("dirname is:" + dirname)

# output file info
for filename in filenames:
print ("parent is:" + parent)
print ("filename is:" + filename)
print ("the full name of the file is:" + os.path.join(parent,filename))

Match based on condition

Posted on 2017-02-20 | Edited on 2018-12-16 | In regex

Problem

Create a regular expression that matches a comma-delimited list of the words one,
two, and three. Each word can occur any number of times in the list, and the words
can occur in any order, but each word must appear at least once.

Solution

\b(?:(?:(one)|(two)|(three))(?:,|\b)){3,}(?(1)|(?!))(?(2)|(?!))(?(3)|(?!))

  • Regex options: None
  • Regex flavors: .NET, PCRE, Perl, Python

First an words anchor, then no capture group, inside three possible group anticipate be matched.
Behind is a comma , or an anchor, these pattern happen at least three times.
If group 1th 2th 3th group exists then txt matched.
if not then (?!) anticipate it’s back is not a null, so definitely fail, no matched.


\b(?:(?:(one)|(two)|(three))(?:,|\b)){3,}

  • Regex options: None
  • Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

Discussion

  • .NET, Python, and PCRE 6.7 (?(name)then|else)
  • Perl 5.10 (?(<name>)then|else) or (?('name')then|else)
  • PCRE 7.0 supports Perl’s syntax for named conditional, .NET, Python as well.
  • .NET, PCRE, and Perl support lookaround (?(?=if)then|else) or (?(?<=if)then|else)

Example

(a)?b(?(1)c|d)1 matches abc and bd
(a)?b(?(?<=ab)c|d) matches abc and bd

tip

lookaround can be written without the conditional as (?=if)then|(?!if)else

(a)?b((?<=ab)c|(?<!ab)d) matches abc and bd

Lookahead and lookbehind

Posted on 2017-02-19 | Edited on 2018-12-16 | In regex

(?<=<b>)\w+(?=</b>)

  • Regex options: Case insensitive
  • Regex flavors: .NET, Java, PCRE, Perl, Python, Ruby 1.9

JavaScript and Ruby 1.8 support the lookahead (?=</b>)
, but not the lookbehind (?<=<b>)

  • I prefer call lookbehind that where ahead cursor is something
  • I prefer call lookahead that where behind cursor is something

Positive lookaround

  • Essentially, lookaround checks whether certain text can be
    matched without actually matching it.

  • lookbehind (?<=…) is the only regular expression construct that
    will traverse the text right to left instead of from left to right

  • Lookaround constructs are therefore called zero-length assertions.

Negative lookaround

  • (?!...) (?<!...), with an exclamation point
    instead of an equals sign, is negative lookaround.

  • negative lookaround matches when the regex inside the
    lookaround fails to match.

Different levels of lookbehind

  • lookahead is completely compatible, even
    lookahead or lookbehind nested in lookahead.

  • lookbehind is different, because regex is design traverse
    from left to right, but lookbehind needs right to left


  1. Perl and Python still require lookbehind to have a fixed length

  2. PCRE and Ruby 1.9 allow alternatives of different lengths inside lookbehind
    notepad++ use PCRE7.2 regular expression engine?

  3. Java takes lookbehind one step further, allows any finite-length
    regular expression ‹*›, ‹+›, and ‹{42,}› inside lookbehind

  4. .NET Framework is the only one in the world
    that can actually apply a full regular expression from right to left.


Lookaround is atomic

(?=(\d+))\w+\1

  • Regex options: None
  • Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

The group capture inside the lookaround is same as usual group,
numbered from outter to inner , left to right

Alternative to Lookbehind

<b>\K\w+(?=</b>)

  • Regex options: Case insensitive
  • Regex flavors: PCRE 7.2, Perl 5.10

Match with ‘\K’, string in front of it will not be pattern.
It matches like a block, no recursive, no loop, no backtrack.

For example:
when (?<=a)a matches the string ‘aaaa’, three a be matched,
the 2th/ 3th/ 4th a. Lookbehind will track to left one matched then next.

But a\Ka matches two a, the 2th and the 4th.
when first/second a captured, abandon first, then second matches.
Then begin next matching, third/fourth a captured, abandon thrid.

Solution Without Lookbehind

In Ruby 1.8 or JavaScript there is no lookbehind can be use.
Solution:

  • use a common expression to suit, group them, just pick the group you want.
  • If replace operation needed, use group number to replace
    which place you don’t want be changed \1 or \kxxx.

simulate lookbehind

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
var mainregexp = /\w+(?=<\/b>)/;
var lookbehind = /<b>$/;
if (match = mainregexp.exec("My <b>cat</b> is furry")) {
// Found a word before a closing tag </b>
var potentialmatch = match[0];
var leftContext = match.input.substring(0, match.index);
if (lookbehind.exec(leftContext)) {
// Lookbehind matched:
// potentialmatch occurs between a pair of <b> tags
} else {
// Lookbehind failed: potentialmatch is no good
}
} else {
// Unable to find a word before a closing tag </b>
}
  • first find the target location with Lookahead, remove it.
  • second if the forepart is end with what you lookbehind anticipated <b>, then lookbehind matched.

Linux display power management

Posted on 2017-02-16 | Edited on 2018-12-16 | In linux

Gnome

/etc/profile.d

a b
xset -q 查询
xset s off 禁用屏幕保护
xset s 3600 3600 设置空闲时间为1小时
xset -dpms 关闭 DPMS[电源之星]
xset +dpms 开启 DPMS[电源之星]
xset s off -dpms 禁用 DPMS 并阻止屏幕进入空闲
xset dpms force off 立即关闭屏幕
xset dpms force standby 强制屏幕进入待命状态
xset dpms force suspend 强制屏幕进入暂停状态
xset dpms 600 900 1200 三个数值分别为 Standby Suspend Off[s]

Command line

Raspbian & Archlinux -> /etc/bash.bashrc
CentOS -> /etc/bashrc

1
2
3
setterm -blank [0-60|force|poke]
setterm -powersave [on|vsync|hsync|powerdown|off]
setterm -powerdown [0-60]

setterm -blank 0 -powerdown 0
No protect mode , no powerdown close display

Linux init task

Posted on 2017-02-15 | Edited on 2018-12-16 | In linux

Modify rc.local

/etc/rc.local add command before exit 0
script must have execute authority x

update-rc.d command

1
2
3
4
5
6
sudo cp -f /home/pi/autowifi.sh /etc/init.d/
[ln -s /home/pi/autowifi.sh /etc/init.d/autowifi]
sudo chmod +x /etc/init.d/autowifi.sh
sudo chown root:root /etc/init.d/autowifi.sh
sudo update-rc.d autowifi.sh defaults 99
sudo update-rc.d -f autowifi.sh remove
  1. add file or link to /etc/init.d
  2. add execute authority to script
  3. add user authority to script
  4. command add script to init start
  5. defaults [num] mean start sequence, the bigger the latter.
1
2
update-rc.d [-n] [-f] name remove
update-rc.d [-n] name default [NN | SS KK]
  • -n do nothing, only preview.
  • -f delete link, even script remain in /etc/init.d
  • NN execute sequence
  • SS begin sequence KK end sequence

Lazy Greedy Selfish

Posted on 2017-02-14 | Edited on 2020-09-17 | In regex

Repeat Certain Number

Hexadecimal number

\b[a-f0-9]{1,8}\b

  • Regex options: Case insensitive
  • Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

Floating-point number

\d*\.\d+(e\d+)?

  • Regex options: Case insensitive
  • Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

Lazy quantifiers

1
2
3
4
*?
+?
??
{7,42}?
  1. any times, one after another, the less the better
  2. more than one time, one after another, the less the better
  3. have or not have, not have first
  4. seven to fourtytwo times, the less the better

In default repeat like * + ? {} after match unit are all greedy[greedy is not selfish]

Possessive quantifiers

Possessive quantifiers is greedy and selfish
It’s greedy and selfish, when it matches, it wouldn’t care other expressions’ express.
In other words, it wouldn’t Backtracking when matches.

1
2
3
4
*+
++
?+
{7,42}+
  1. any times, block reduce to match, the more the better, no backtracking
  2. more than one time, the more the better, no backtracking
  3. have or not have, the more the better, no backtracking
  4. seven to fourtytwo times, the more the better, no backtracking

\b\d++\b

  • Regex options: None
  • Regex flavors: Java, PCRE, Perl 5.10, Ruby 1.9

\b(?>\d+)\b

  • Regex options: None
  • Regex flavors: .NET, Java, PCRE, Perl, Ruby

ps. (?>\d+) is an atomic group that essentially is a noncapturing group,
with the extra job of refusing to backtrack.

Example

\w++\d++ can’t mathes “123abc”
Because \w++ selfishly occupy “123abc”, don’t care \d++ failed to match.
When use (\w+)(\d+) to match “123abc”, i found gourp \1 matches “12”
and group \2 matches “3”, so greedy works.
It’s not mean selfish is bad, but only unusual work in simply task.

Prevent Runaway Repetition

Why prevent? because in some complex condition, expression will complex the same.
In these condition Possessive quantifiers is necessary.

Problem

Match a html’s origin code
Solution

1
2
<html>(?>.*?<head>)(?>.*?<title>)(?>.*?</title>)↵
(?>.*?</head>)(?>.*?<body[^>]*>)(?>.*?</body>).*?</html>

  • Regex options: Case insensitive, dot matches line breaks
  • Regex flavors: .NET, Java, PCRE, Perl, Ruby

ps.If some tag properties need be matched, please modify expression.

This regular expression has a worst-case complexity$^3$ of $O(n^7)$
If the file is twice the size, the regex can need up to 128 times
as many steps to figure out it doesn’t match.

Example

(x+x+)+y to matches “xxxxxxxxxx”
It should not matches, watching the operation progress.
This regex is $O(2^n)$, if one more ‘x’ is added, the regex can need up to 2times

Keyboard special characters

Posted on 2017-02-14 | Edited on 2020-09-17 | In tool

sign

1
2
3
4
|
|
|
|
a b
` backquote 反引号
~ tilde
! exclam
@ at
# numbersign,英语国家是hash</br>美语是pound</br>音乐里作sharp,如C#
$ dollar
% percent
^ caret
& ampersand
* asterisk,star(美语),数学公式中作multiply
( parenleft,opening parentheses
) parenright,closing paretheses
- minus;hyphen连字符,不读
_ underscore
+ plus
= equal
[ bracketleft,opening bracket
] bracketright,closing bracket
{ braceleft
} braceright
; semicolon
: colon
‘ quote
“ doublequote
/ slash
\ backslash 反斜杠
| bar
, comma
< less angle brackets
> greater angle brackets
. period
? question
space

Group match

Posted on 2017-02-13 | Edited on 2020-09-17 | In regex

Group number rules

\b(\d\d(\d\d))-\2-\2\b
From outer to inner , from left to right number increase start from 1.

Noncapturing group

\b(?i:Mary|Jane|Sue)\b

  • Regex options: None
  • Regex flavors: .NET, Java, PCRE, Perl, Ruby

(?i-sm:Mary.*?\r\n)

  • Regex options: None
  • Regex flavors: .NET, Java, PCRE, Perl, Ruby

Inner config the group capture options
parentheses start like ‘?:’ ‘?xxx:’, means group match but no capture.

Match Previously Matched Again

\b\d\d(\d\d)-\1-\1\b

  • Regex options: None
  • Regex flavors: .NET, Java, JavaScript, PCRE, Perl, Python, Ruby

\01 is either an octal escape or an error, dont’t use it.
\xFF hexadecimal escapes are much easier to understand

JavaScript if the \1 appear in front of group it match like a zero-length
\d\d\1-\1-(\d\d) can match 12--34
after ‘2’ and ‘-‘ two zero-length position matches.

Name the capture group

Named capture

\b(?<year>\d\d\d\d)-(?<month>\d\d)-(?<day>\d\d)\b

  • Regex options: None
  • Regex flavors: .NET, Java 7, XRegExp, PCRE 7, Perl 5.10, Ruby 1.9

\b(?'year'\d\d\d\d)-(?'month'\d\d)-(?'day'\d\d)\b

  • Regex options: None
  • Regex flavors: .NET, PCRE 7, Perl 5.10, Ruby 1.9

\b(?P<year>\d\d\d\d)-(?P<month>\d\d)-(?P<day>\d\d)\b

  • Regex options: None
  • Regex flavors: PCRE 4 and later, Perl 5.10, Python

Named backreferences

\b\d\d(?<magic>\d\d)-\k<magic>-\k<magic>\b

  • Regex options: None
  • Regex flavors: .NET, Java 7, XRegExp, PCRE 7, Perl 5.10, Ruby 1.9

\b\d\d(?'magic'\d\d)-\k'magic'-\k'magic'\b

  • Regex options: None
  • Regex flavors: .NET, PCRE 7, Perl 5.10, Ruby 1.9

\b\d\d(?P<magic>\d\d)-(?P=magic)-(?P=magic)\b

  • Regex options: None
  • Regex flavors: PCRE 4 and later, Perl 5.10, Python

Group’s name must consist of word characters matched by \w

Angle brackets not friend in xml style environment.
So some language support use in ' single quotes instead of angle brackets.

Use same name in expression is not recommanded

Syntax history

Python was the first regular expression flavor to support named capture.

Perhaps due to .NET’s popularity over Python, the .NET syntax seems to be the one
that other regex library developers prefer to copy. Perl 5.10 and later have it, and so
does the Oniguruma engine in Ruby 1.9. Perl 5.10 and Ruby 1.9 support both the syntax
using angle brackets and single quotes. Java 7 also copied the .NET syntax, but only
the variant using angle brackets. Standard JavaScript does not support named capture.
XRegExp adds support for named capture using the .NET syntax, but only the variant
with angle brackets.

PCRE copied Python’s syntax long ago, at a time when Perl did not support named
capture at all. PCRE 7, the version that adds the new features in Perl 5.10, supports
both the .NET syntax and the Python syntax. Perhaps as a testament to the success of
PCRE, in a reverse compatibility move, Perl 5.10 also supports the Python syntax. In
PCRE and Perl 5.10, the functionality of the .NET syntax and the Python syntax for
named capture is identical.

Hello Raspberry

Posted on 2017-02-11 | Edited on 2018-12-16 | In raspberry

First Git commit by raspberry

Interesting! a lot of fun
Bug in 2017-02-02
Raspberry Pi -Read 20Mb/s Write 20Mb/s in board tf card

change keyboard layout

1
2
3
sudo nano /etc/default/keyboard

XKBLAYOUT="us"

setting location&timezone

1
sudo raspi-config

wireless ctl

1
2
3
4
5
6
sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
network={
ssid="TP-LINK XXX"
psk="i'm password"
key_mgmt=WPA-PSK
}

if not work first check ssid & passwd correct
then ckeck net card status

1
2
iwconfig
ifconfig wlan0

try manually restart the interface with
sudo ifdown wlan0 and sudo ifup wlan0
at last try sudo reboot

Version of javascript

Posted on 2017-02-09 | Edited on 2020-09-17 | In javascript
Version Description
JavaScript 1.0 The original version of the language. It was buggy and is now essentially obsolete. Implemented by Netscape 2.
JavaScript 1.1 Introduced a true Array object; most serious bugs resolved. Implemented by Netscape 3.
JavaScript 1.2 Introduced the switch statement, regular expressions, and a number of other features. Almost compliant with ECMA v1, but has some incompatibilities. Implemented by Netscape 4.
JavaScript 1.3 Fixed incompatibilities of JavaScript 1.2. Compliant with ECMA v1. Implemented by Netscape 4.5.
JavaScript 1.4 Implemented only in Netscape server products.
JavaScript 1.5 Introduced exception handling. Compliant with ECMA v3. Implemented by Mozilla and Netscape 6.
JScript 1.0 Roughly equivalent to JavaScript 1.0. Implemented by early releases of IE 3.
JScript 2.0 Roughly equivalent to JavaScript 1.1. Implemented by later releases of IE 3.
JScript 3.0 Roughly equivalent to JavaScript 1.3. Compliant with ECMA v1. Implemented by IE 4.
JScript 4.0 Not implemented by any web browser.
JScript 5.0 Supported exception handling. Partially compliant with ECMA v3. Implemented by IE 5.
JScript 5.5 Roughly equivalent to JavaScript 1.5. Fully compliant with ECMA v3. Implemented by IE 5.5 and IE 6. (IE 6 actually implements JScript 5.6, but 5.6 is not different from 5.5 in any way that is relevant to client-side JavaScript programmers.)
ECMA v1 The first standard version of the language. Standardized the basic features of JavaScript 1.1 and added a few new features. Did not standardize the switch statement or regular expression support. Conformant implementations are JavaScript 1.3 and JScript 3.0.
ECMA v2 A maintenance release of the standard that included clarifications but defined no new features.
ECMA v3 Standardized the switch statement, regular expressions, and exception handling. Conformant implementations are JavaScript 1.5 and JScript 5.5.
1…242526…29

Leon

282 posts
20 categories
58 tags
GitHub
Links
  • clock
  • typing-cn
  • mathjax
  • katex
  • cron
  • dos
  • keyboard
  • regex
  • sql
  • toy
© 2017 – 2024 Leon
Powered by Hexo v3.9.0
|
Theme – NexT.Muse v7.1.2