正则表达式匹配坐标以零结尾的线

时间:2017-05-18 00:34:32

标签: regex

给出以下内容:

 1803            1004    -4.2
 1807            1005    3.3
 1809            1006    -8.9
 1800            1007    -3.7
 1805            1008    9.1
 1808            1009    -4.3
 1800            1000    3.2

我希望正则表达式将一条线与两个以零结尾的第一个坐标匹配,所以我们只返回:

1800            1000    3.2

我只想要前两个数字都以零结尾的行,是的,这些行在开头或数字之间会有大量的空格。

我尝试过'\ s * \ d + 0 \ z * \ d + 0 *'和' \ d + 0 \ s \ d + 0 *'的各种组合结果。

我正在将它与grep结合使用。

2 个答案:

答案 0 :(得分:1)

尝试一下:import nfc import time import os def typestr(text,returnatend): totype = text if returnatend=="false": cmd = """osascript -e 'tell application "System Events"' -e 'delay 0.1' -e 'keystroke "%s"' -e 'end tell' -e 'delay 2.0'""" % totype else: cmd = """osascript -e 'tell application "System Events"' -e 'delay 0.1' -e 'keystroke "%s"' -e 'delay 0.1' -e 'key code 36' -e 'end tell' -e 'delay 2.0'""" % totype os.system(cmd) def notification_osx(text,title,subtitle): cmd = """osascript -e 'display notification "%s" with title "%s" subtitle "%s"'""" % (text, title, subtitle) os.system(cmd) def on_startup(targets): for target in targets: target.sensf_req = bytearray.fromhex("0012FC0000") return targets def on_connect(tag): print("printing tag") print(tag) print("printing uid") uid = str(tag.identifier).encode("hex").upper() typestr(uid,"false") notification_osx(uid,"Scanned Card", "Scanned a card with ID:") rdwr_options = { 'on-startup': on_startup, 'on-connect': on_connect, 'beep-on-connect': False, } while True: with nfc.ContactlessFrontend('usb') as clf: tag = clf.connect(rdwr=rdwr_options)

行动中:https://regex101.com/r/t0hhDL/2

从您的问题中不清楚您使用的数据是否都是一个大字符串,或者这些是返回的多行。我假设后者有上面的答案,但如果情况并非如此,那么模式将需要略有不同。

答案 1 :(得分:1)

我推荐grep中的选项:final String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath(); buttonPlayLastRecordAudio.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) throws IllegalArgumentException, SecurityException, IllegalStateException { buttonStop.setEnabled(false); buttonStart.setEnabled(false); buttonStopPlayingRecording.setEnabled(true); mediaPlayer = new MediaPlayer(); try { mediaPlayer.setDataSource(baseDir); mediaPlayer.prepare(); } catch (IOException e) { e.printStackTrace(); } mediaPlayer.start(); Toast.makeText(SendActivity.this, "Recording Playing", Toast.LENGTH_LONG).show(); } });

-E

结果:

enter image description here

行动中:https://regex101.com/r/h4on2q/1

另外,

关于$ grep -E '^ *([0-9]*0) +([0-9]*0) +.*$' dataFile -E

$ man grep

基本与扩展正则表达式: https://www.gnu.org/software/grep/manual/html_node/Basic-vs-Extended.html