使用正则表达式

时间:2015-09-20 13:38:46

标签: python regex python-2.x

我想使用匹配两个字符串之间任何文本的正则表达式:

Part 1. Part 2. Part 3 then more text

在这个例子中,我想搜索"第1部分"和"第3部分"然后得到介于两者之间的所有内容:"。第2部分。"

我正在使用Python 2x。

1 个答案:

答案 0 :(得分:20)

使用<Application x:Class="MVCP.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dragablz="clr-namespace:Dragablz;assembly=Dragablz" StartupUri="FloatingActivator.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <!-- primary color --> <ResourceDictionary> <!-- include your primary palette --> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.Indigo.xaml" /> </ResourceDictionary.MergedDictionaries> <!-- include three hues from the primary palette (and the associated forecolours). Do not rename, keep in sequence; light to dark. --> <SolidColorBrush x:Key="PrimaryHueLightBrush" Color="{StaticResource Primary100}"/> <SolidColorBrush x:Key="PrimaryHueLightForegroundBrush" Color="{StaticResource Primary100Foreground}"/> <SolidColorBrush x:Key="PrimaryHueMidBrush" Color="{StaticResource Primary500}"/> <SolidColorBrush x:Key="PrimaryHueMidForegroundBrush" Color="{StaticResource Primary500Foreground}"/> <SolidColorBrush x:Key="PrimaryHueDarkBrush" Color="{StaticResource Primary700}"/> <SolidColorBrush x:Key="PrimaryHueDarkForegroundBrush" Color="{StaticResource Primary700Foreground}"/> </ResourceDictionary> <!-- secondary colour --> <ResourceDictionary> <!-- include your secondary pallette --> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/MaterialDesignColor.Yellow.xaml" /> </ResourceDictionary.MergedDictionaries> <!-- include a single secondary accent color (and the associated forecolour) --> <SolidColorBrush x:Key="SecondaryAccentBrush" Color="{StaticResource Accent200}"/> <SolidColorBrush x:Key="SecondaryAccentForegroundBrush" Color="{StaticResource Accent200Foreground}"/> </ResourceDictionary> <!-- Include the Dragablz Material Design style --> <ResourceDictionary Source="pack://application:,,,/Dragablz;component/Themes/materialdesign.xaml"/> </ResourceDictionary.MergedDictionaries> <Style x:Key="OpenSans"> <Setter Property="TextElement.FontFamily" Value="Open Sans, /MVCP;component/Fonts/#Open Sans" /> </Style> <!-- tell Dragablz tab control to use the Material Design theme --> <Style TargetType="{x:Type dragablz:TabablzControl}" BasedOn="{StaticResource MaterialDesignTabablzControlStyle}" /> <Style x:Key="FileItemStyle" TargetType="{x:Type ListViewItem}"> <Setter Property="Margin" Value="5,5,5,5"/> <Setter Property="Padding" Value="0,0,0,0"/> <Setter Property="HorizontalAlignment" Value="Left"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ListViewItem}"> <Grid HorizontalAlignment="Left" VerticalAlignment="Top" Height="50" > <Border x:Name="border" BorderBrush="{x:Null}" BorderThickness="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CornerRadius="2.5"/> <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <ContentPresenter/> </StackPanel> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> </Application.Resources>

re.search

如果有多个出现,请使用>>> import re >>> s = 'Part 1. Part 2. Part 3 then more text' >>> re.search(r'Part 1\.(.*?)Part 3', s).group(1) ' Part 2. ' >>> re.search(r'Part 1(.*?)Part 3', s).group(1) '. Part 2. '