查找匹配单词并在其旁边打印字母

时间:2017-10-30 22:40:03

标签: python regex text

我试图在字符串上找到一个单词,将其与查询字匹配,然后用它们的一些相邻字母打印出来,如下所示:

import re
teststring = "aaxxYYxxaa"
word = re.findall (r"YY", teststring)
print(word)

到目前为止,我已尝试使用Regex模块,但我不能超越'匹配'部分:

2017-10-30 19:23:32.237209-0300 Club PO[10332:4430711] FATAL: OneSignal AppId format is invalid.
Example: 'b2f7f966-d8cc-11e4-bed1-df8f05be55ba'
2017-10-30 19:23:32.243192-0300 Club PO[10332:4430711] Apache Cordova native platform version 4.3.1 is starting.
2017-10-30 19:23:32.243305-0300 Club PO[10332:4430711] Multi-tasking -> Device: YES, App: YES
2017-10-30 19:23:32.373394-0300 Club PO[10332:4430711] [MC] Lazy loading NSBundle MobileCoreServices.framework
2017-10-30 19:23:32.374762-0300 Club PO[10332:4430711] [MC] Loaded MobileCoreServices.framework
2017-10-30 19:23:32.380800-0300 Club PO[10332:4430711] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
2017-10-30 19:23:32.389913-0300 Club PO[10332:4430711] Using UIWebView
2017-10-30 19:23:32.392638-0300 Club PO[10332:4430711] [CDVTimer][handleopenurl] 0.105977ms
2017-10-30 19:23:32.395315-0300 Club PO[10332:4430711] [CDVTimer][intentandnavigationfilter] 2.561986ms
2017-10-30 19:23:32.395519-0300 Club PO[10332:4430711] [CDVTimer][gesturehandler] 0.064969ms
2017-10-30 19:23:32.508949-0300 Club PO[10332:4430711] [CDVTimer][splashscreen] 113.343000ms
2017-10-30 19:23:32.523217-0300 Club PO[10332:4430711] [CDVTimer][statusbar] 14.133990ms
2017-10-30 19:23:32.526091-0300 Club PO[10332:4430711] [CDVTimer][keyboard] 2.716005ms
2017-10-30 19:23:32.526343-0300 Club PO[10332:4430711] Starting Facebook Connect plugin
2017-10-30 19:23:32.526430-0300 Club PO[10332:4430711] [CDVTimer][facebookconnectplugin] 0.211000ms
2017-10-30 19:23:32.526570-0300 Club PO[10332:4430711] [CDVTimer][TotalPluginStartup] 134.099007ms
2017-10-30 19:23:32.667525-0300 Club PO[10332:4430711] refreshPreferences: HangTracerEnabled: 0
2017-10-30 19:23:32.667577-0300 Club PO[10332:4430711] refreshPreferences: HangTracerDuration: 500
2017-10-30 19:23:32.667602-0300 Club PO[10332:4430711] refreshPreferences: ActivationLoggingEnabled: 0 ActivationLoggingTaskedOffByDA:0
2017-10-30 19:23:33.065090-0300 Club PO[10332:4430711] [MC] Reading from public effective user settings.
2017-10-30 19:23:33.113662-0300 Club PO[10332:4430711] Resetting plugins due to page load.
2017-10-30 19:23:33.689447-0300 Club PO[10332:4430787] TIC Read Status [1:0x0]: 1:57
2017-10-30 19:23:33.690025-0300 Club PO[10332:4430787] TIC Read Status [1:0x0]: 1:57
2017-10-30 19:23:53.167722-0300 Club PO[10332:4430786] NSURLConnection finished with error - code -1001
2017-10-30 19:23:53.186367-0300 Club PO[10332:4430711] Failed to load webpage with error: The request timed out.
2017-10-30 19:23:53.191988-0300 Club PO[10332:4430787] Task <8DE2F0BC-14FC-47A8-9049-99895860ED86>.<0> HTTP load failed (error code: -999 [1:89])

输出= YY

我可以在这里打印'YY'字两端的字母?

谢谢。

2 个答案:

答案 0 :(得分:0)

看起来好像要匹配Imports System.Drawing Imports Emgu.CV Imports Emgu.CV.Face Imports Emgu.CV.Structure Imports Emgu.CV.Util Public Class _Default Inherits Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load End Sub Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim images As New Mat Dim Palb0 = "c://IMGDB//Alberto//0.jpg" Dim Palb1 = "c://IMGDB//Alberto//1.jpg" Dim Pcon0 = "c://IMGCONFRONTO//0.jpg" images.PushBack(CvInvoke.Imread(Palb0, CvEnum.LoadImageType.Grayscale)) images.PushBack(CvInvoke.Imread(Palb1, CvEnum.LoadImageType.Grayscale)) Dim model = New EigenFaceRecognizer(80, Double.PositiveInfinity) Dim labels As New VectorOfInt Dim a(0) As Integer a(0) = 0 labels.Push(a) model.Train(images, labels) Dim imgConf As Mat imgConf = CvInvoke.Imread(Pcon0, CvEnum.LoadImageType.Grayscale) model.Predict(imgConf) Dim PR As FaceRecognizer.PredictionResult Dim dst = PR.Distance Dim lbl = PR.Label MsgBox(dst) End Sub End Class 值之前和之后的任何0到2个字符。在模式的两侧添加YY

.{0,2}

请参阅regex demoPython demo

re.findall(r".{0,2}YY.{0,2}", teststring)

答案 1 :(得分:0)

你会以某种方式编写你的正则表达式,它在你知道搜索词之前和之后匹配任意字符。

.匹配任何字符 {m,n}重复至少m次,最多n次

所以为匹配xxYYxx,您会说.{2,2}YY.{2,2}