Get String Between Two Strings in a String

How to get a substring between two strings in PHP?

If the strings are different (ie: [foo] & [/foo]), take a look at this post from Justin Cook.
I copy his code below:

function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}

$fullstring = 'this is my [tag]dog[/tag]';
$parsed = get_string_between($fullstring, '[tag]', '[/tag]');

echo $parsed; // (result = dog)

Find string between two substrings


import re

s = 'asdf=5;iwantthis123jasd'
result = re.search('asdf=5;(.*)123jasd', s)
print(result.group(1))

Get string between two strings Swift

Use components(separatedBy:):

var str = "Notes[9219:1224244] [BoringSSL] Function boringssl_context_get_peer_sct_list: line 1757 received sct extension length is less than sct data length [[[\"encendedor\",\"lighter\",null,null,1]],null,\"en\"]"

str.components(separatedBy: "\"")[1] // "encendedor"

Get string between two strings

Regular expressions

import re
matches = re.findall(r'<p>.+?</p>',string)

The following is your text run in console.

>>>import re
>>>string = """<p>I'd like to find the string between the two paragraph tags.</p><br><p>And also this string</p>"""
>>>re.findall('<p>.+?</p>',string)
["<p>I'd like to find the string between the two paragraph tags.</p>", '<p>And also this string</p>']

Go: Retrieve a string from between two characters or other strings

There are lots of ways to split strings in all programming languages.

Since I don't know what you are especially asking for I provide a sample way to get the output
you want from your sample.

package main

import "strings"
import "fmt"

func main() {
initial := "<h1>Hello World!</h1>"

out := strings.TrimLeft(strings.TrimRight(initial,"</h1>"),"<h1>")
fmt.Println(out)
}

In the above code you trim <h1> from the left of the string and </h1> from the right.

As I said there are hundreds of ways to split specific strings and this is only a sample to get you started.

Hope it helps, Good luck with Golang :)

DB

Java Get String Between Two Strings

You can use regex to accomplish this, by searching for the ~PHONE_characters=digits pattern, like so:

String str = "~PHONE_IDX=200~PHONE_DD=100~PHONE_KK=50~";
Pattern p = Pattern.compile("~PHONE_(?<attribute>\\w+)=(?<value>\\d+)");
Matcher m = p.matcher(str);//matcher for string
while(m.find())
{
System.out.println("Next group: "+m.group());
System.out.println("Attribute: "+m.group("attribute"));
System.out.println("Value: "+m.group("value"));
}

This code will output the following:

Next group: ~PHONE_IDX=200
Attribute: IDX
Value: 200
Next group: ~PHONE_DD=100
Attribute: DD
Value: 100
Next group: ~PHONE_KK=50
Attribute: KK
Value: 50

Get string between two strings - first string ends with newline

In general, you may use a pattern like

/--START\s*(.*?)\s*--END/s

See the regex demo. \s* will match any 0+ whitespaces, but it won't require line breaks after --START and before --END.

A bit more specific pattern will be

/--START\h*\R\s*(.*?)\h*\R\s*--END/s

Or, if the --START and --END should appear at the start of lines, add anchors and m modifier:

/^--START\h*\R\s*(.*?)\h*\R\s*--END$/sm

See the regex demo and another regex demo.

Details

  • ^ - start of a line (since m modifier is used)
  • --START - left-hand delimiter
  • \h* - 0+ horizontal whitespaces
  • \R - a line break
  • \s* - 0+ whitespaces
  • (.*?) - Group 1: any 0+ chars, as few as possible
  • \h* - 0+ horizontal whitespaces
  • \R - a linebreak
  • \s* - 0+ whitespaces
  • --END - the right-hand delimiter
  • $ - end of a line.


Related Topics



Leave a reply



Submit