Regex- Extract occurrence between to delimiters

时间:2017-08-30 20:20:38

标签: regex google-data-studio

How can I extract subsequent occurrence in this pattern:

DisplayMetrics

So for example I want to extract 3rd occurrence only, I've tried to use:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics); //the error is on all 3 methods here
int height = metrics.heightPixels;
int width = metrics.widthPixels;

But it extracts from 3rd occurrence and everything after it.

1 个答案:

答案 0 :(得分:0)

The REGEXP_EXTRACT reference shows an example of using a capturing group to extract the portion of a string you need.

So, match up to the 3rd part, and then capture the 3rd value:

<div contenteditable="true">
  <span style='background-color: lightgreen'>ab</span><span data-end></span>c<span style='background-color: lightgreen'>ab</span>
</div>

Here is a demo, the green highlighted part will get extracted.

Details

  • <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp1.0</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Mono.Data.OracleClientCore" Version="1.0.0" /> </ItemGroup> </Project> - start of string
  • using System; using System.Data.OracleClient; namespace TestCore { class Program { static void Main(string[] args) { Console.WriteLine("Starting.\r\n"); using (var _db = new OracleConnection("User Id=myUser;Password=myPassword;Data Source=MyOracleConnection")) { Console.WriteLine("Open connection..."); _db.Open(); Console.WriteLine( "Connected to:" +_db.ServerVersion); Console.WriteLine("\r\nDone. Press key for exit"); Console.ReadKey(); } } } } - 2 occurrences of:
    • ^(?:[^|]*[|]){2}([^|]*) - zero or more chars other than ^
    • (?:[^|]*[|]){2} - a [^|]* pipe symbol
  • | - Group 1: zero or more chars other than [|].