正则表达式匹配字符串,直到第一次出现小数点后跟制表符

时间:2018-07-15 15:27:46

标签: php regex

我正在寻找一个正则表达式来实现以下目的:从列表中,我只希望在第一次出现小数点后具有制表符的所有字符串。这些字符串只能在每行的开头。使用当前的正则表达式 //Request a read permission of user's info from Facebook //Data provided by Facebook will be used for Firebase FireStore LoginManager.getInstance().logInWithReadPermissions(LogIn.this, Arrays.asList("email", "public_profile")); LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(final LoginResult loginResult) { mStateOfSuccess = false; //Dismiss any snackbar first before showing a new one mSnackBar.dismiss(); mSnackBar.show(); Log.d(TAG, "facebook:onSuccess:" + loginResult); //Bundle is use for passing data as K/V pair like a Map Bundle bundle=new Bundle(); //Fields is the key of bundle with values that matched the proper Permissions Reference provided by Facebook bundle.putString("fields","id, email, first_name, last_name, gender,age_range"); //Graph API to access the data of user's facebook account GraphRequest request = GraphRequest.newMeRequest( loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted(JSONObject object, GraphResponse response) { Log.v("Login Success", response.toString()); //For safety measure enclose the request with try and catch try { //The get() or getString() key should be included in Bundle otherwise it won't work properly //If not then error dialog will be called //First re-initialize jSON object to a new Contructor with parameter that is equal to a jSON format age range JSONObject ageRange = new JSONObject(object.getString("age_range")); //Log in using Facebook with Firebase loginToFirebaseUsingFacebook(loginResult.getAccessToken() ,object.getString("first_name") ,object.getString("last_name") //Then get again get a string from object itself for the minimum age range //The idea is that we need to get minimum age only written in string format //not the whole age range data that is written in jSON format ,ageRange.getString("min") ,object.getString("gender") ,object.getString("email") ); } //If no data has been retrieve throw some error catch (JSONException e) { ErrorDialog(e.getMessage(),"facebookAuth"); } } }); //Set the bundle's data as Graph's object data request.setParameters(bundle); //Execute this Graph request asynchronously request.executeAsync(); } @Override public void onCancel() { Log.d(TAG, "facebook:onCancel"); ErrorDialog("Request has canceled.","facebookAuth"); } @Override public void onError(FacebookException error) { Log.d(TAG, "facebook:onError", error); ErrorDialog(String.valueOf(error),"facebookAuth"); } }); } ,我什至得到的字符串确实具有2个或多个小数点。

下面是一些示例代码(空格实际上是制表符):

PermissionError: [Errno 13] Permission denied: 'GameExcel.xlsx'

我需要的是import openpyxl from openpyxl import load_workbook from openpyxl import workbook from openpyxl.utils import get_column_letter import os import tkinter as tk from tkinter import messagebox as tkMsgBox import time os.chdir("D:\Scripts\Python\Testing Scripts\My Excel Game") wb = load_workbook("GameExcel.xlsx") names = wb.sheetnames sheet = wb['GameEnviroment'] #userInput = (input("what would you like it to say?")) #print(userInput) C3Val = sheet['C4'].value sheet.cell(row=3, column=4).value = (C3Val + ' 4') wb.save('GameExcel.xlsx') print(C3Val + ' 3') #sheet['A1']=userInput /^(\S+\.)/gmaaa. 86400 ns1.dns.nic.aaa. 172800 IN A 156.154.144.2 ns1.dns.nic.aaa. 172800 IN AAAA 2610:a1:1071:0:0:0:0:2 abarth. 86400 IN RRSIG NSEC 8 1 86400 20180728050000 20180715040000 41656 . a0.nic.abarth. 172800 IN A 65.22.24.17 a0.nic.abarth. 172800 IN AAAA 2a01:8840:1a:0:0:0:0:17 ai. ns2.offshore.ai. 172800 IN A 108.166.113.245 whois.ai. 172800 IN A 209.59.119.1 xn--node.ns.anycast.pch.net. 172800 IN A 204.61.216.88 d.nic.xn--mxtq1m. 172800 IN AAAA 2001:c50:ffff:1:0:0:0:185 d.nic. xn--ngbc5azd. 172800 IN NS a.nic.xn--ngbc5azd. aaa.,而不是abarth.ai.或其他任何带有超过1个小数点。

谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用

'~^[^.\r\n]+\.(?=\t)~m'

请参见regex demo注意:如果将行作为单独的字符串传递,则不需要\r\nm修饰符。

详细信息

  • ^-行首(因为m修饰符使^匹配行首)
  • [^.\r\n]+-除.,LF和CR之外的1个以上的字符
  • \.-一个点
  • (?=\t)-.之后必须有一个标签。

PHP demo

if (preg_match_all('~^[^.\r\n]+\.(?=\t)~m', $str, $m)) {
    print_r($m[0]);
}

输出:

Array
(
    [0] => aaa.
    [1] => abarth.
    [2] => ai.
    [3] => xn--ngbc5azd.
)

答案 1 :(得分:0)

最新答案,但您也可以使用:

preg_match_all( '/^([^.]+\.)\t/m', $x, $m, PREG_PATTERN_ORDER );
print_r( $m[1] );

Array
(
    [0] => aaa.
    [1] => abarth.
    [2] => ai.
    [3] => xn--ngbc5azd.
)

Regex101
Ideone