python中的正则表达式:如何在模式中使用变量字符串?

时间:2016-07-14 13:12:44

标签: python regex

我可以匹配字符串列表中的字符串,如下所示:

keys = ['a', 'b', 'c']
for key in keys:
    if re.search(key, line):
       break

问题是我想匹配我将指定的正则表达式+字符串制作的模式。像这样:

keys = ['a', 'b', 'c']
for key in keys:
    if re.search('[^\[]'+key+'[^\]]', line):
       break

但是这不起作用(在这个例子中,我希望匹配' a'' b'以及' c'只有当它们出现在方括号中时)。 我认为这与原始字符串等有关,但我无法找到使其工作的方法。建议?

编辑:

让我们说我希望匹配一个更复杂的模式:

'[^\s*data\.'+key+'\s*=\s*\[(.+)[^\]]'

为了匹配括号中的数字:

 data.a =  [12343.232 ]

1 个答案:

答案 0 :(得分:1)

//add platfom

ionic platform add android
ionic io init
ionic config set dev_push true

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                                    //open ionic io dashboard
go to setting
    1-create api key    
    2-go to certificate and create security profile name edit id click on android and  add GCM key
    and save it.
    3-after this again  edit on your profile and save Android Keystore ,use following command
    "keytool -genkey -v -keystore MAHESH.keystore -alias rigel -keyalg RSA -keysize 2048 -validity 10000"
BUT BEFORE THAT OPEN NEW command prompt aand we have to go to the bin directory of our java sdk
so-cd C:\Program Files\Java\jdk1.7.0\bin 
and then press enter now you are in bin folder and this time run above command without changing it 
we have given the name Mahesh so after running above cammand it will create one file in your bin directory named "mahesh"  
of type
keytool
just go to ionic io  go into setting and edit on profile and add keystore by giving path to the file we created and enter the
 password and enter the 
alise name as "rigel"
(if your password are same for both ten also it is ok i am kind of lazy thats y  have same password)
-save it
(now there is nothing to do in ionic io dashboard)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            INDEX.HTML
    //add this code to app.js

    angular.module('starter', ['ionic'])

    .run(function($ionicPlatform) {
    $ionicPlatform.ready(function() {
     var push = new Ionic.Push({
      "debug": true
        });

       push.register(function(token) {
      console.log("My Device token:",token.token);
      push.saveToken(token);  // persist the token in the Ionic Platform
        });
    });
    })

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                                     POSTMAN
//to test wether your configure correctly with ionic io open launch postman
//and then do the following things:

    1-create collection give it a valid name
    2-in body click on text and select Application/json
    it will add header automatically
    3-add another header     
    key as Authorization
    value as Bearer followed by your api token from ionic io
    4-select "raw " as the format of our json
    2-in body section of your collection write
    following code
    {
    "tokens": ["DEV_DEVICE_TOKEN"],
        "profile": "PROFILE_NAME",
        "notification":
         {
        "message": "This is my demo push!"
         }
    }


//now it will prompt message on browser
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
to make it work on mobile do the following things

1-open ionic io dashboard
2-go to setting 


ionic config set gcm_key   <your-gcm-project-number>    
ionic config set dev_push false
ionic build android 

install your app in mobile and send the notification from postman 
again just change the token and send it
do the following things
1-first run your app in your mobile
2-after that connect it to the pc/laptop via cable
3-in your mobile open "developer mode"
(in some mobile at first you will not see developer option ,
to see that developer option just click on "about phone " more than four times and you will get massage 
"debugger mode on" after you will have that developer option in your mobile)
4-after that in your pc type this URL
" chrome://inspect/#devices"
you will see your phone and inspect option there
to open our ceated app in debugger mode you should open your app in mobile then only you can see your app 
on pc browser.
5- click on inspect go into console and copy that device token 
6-open postman and just change the token and send it

you will get notification on your mobile(make sure you have internet plan activated ) 

(Mahesh Sampat Nighut)

这将匹配re.search('\['+re.escape(key)+']', line): 。请注意,添加了[key]是为了防止re.escape中的字符被解释为正则表达式。