Get Substring between two characters using javascript
You can try this
var mySubString = str.substring(
str.indexOf(":") + 1,
str.lastIndexOf(";")
);
regex to get string between two % characters
Your pattern is fine but your code didn't compile. Try this instead:
Swift 4
let query = "Hello %test% how do you do %test1%"
let regex = try! NSRegularExpression(pattern:"%(.*?)%", options: [])
var results = [String]()
regex.enumerateMatches(in: query, options: [], range: NSMakeRange(0, query.utf16.count)) { result, flags, stop in
if let r = result?.range(at: 1), let range = Range(r, in: query) {
results.append(String(query[range]))
}
}
print(results) // ["test", "test1"]
NSString
uses UTF-16 encoding so NSMakeRange
is called with the number of UTF-16 code units.
Swift 2
let query = "Hello %test% how do you do %test1%"
let regex = try! NSRegularExpression(pattern:"%(.*?)%", options: [])
let tmp = query as NSString
var results = [String]()
regex.enumerateMatchesInString(query, options: [], range: NSMakeRange(0, tmp.length)) { result, flags, stop in
if let range = result?.rangeAtIndex(1) {
results.append(tmp.substringWithRange(range))
}
}
print(results) // ["test", "test1"]
Getting a substring out of Swift's native String
type is somewhat of a hassle. That's why I casted query
into an NSString
Extracting a string between other two strings in R
You may use str_match
with STR1 (.*?) STR2
(note the spaces are "meaningful", if you want to just match anything in between STR1
and STR2
use STR1(.*?)STR2
, or use STR1\\s*(.*?)\\s*STR2
to trim the value you need). If you have multiple occurrences, use str_match_all
.
Also, if you need to match strings that span across line breaks/newlines add (?s)
at the start of the pattern: (?s)STR1(.*?)STR2
/ (?s)STR1\\s*(.*?)\\s*STR2
.
library(stringr)
a <- " anything goes here, STR1 GET_ME STR2, anything goes here"
res <- str_match(a, "STR1\\s*(.*?)\\s*STR2")
res[,2]
[1] "GET_ME"
Another way using base R regexec
(to get the first match):
test <- " anything goes here, STR1 GET_ME STR2, anything goes here STR1 GET_ME2 STR2"
pattern <- "STR1\\s*(.*?)\\s*STR2"
result <- regmatches(test, regexec(pattern, test))
result[[1]][2]
[1] "GET_ME"
How to get the substring between two markers in Python multiple times?
you can use regex
import re
s = '''alt="Thunder Force"/>ehkjehkljhiflealt="Godzilla vs. Kong"/>'''
x = re.findall(r'alt="(.*?)"/>', s)
print(x)
output
['Thunder Force', 'Godzilla vs. Kong']
Find string between two substrings
import re
s = 'asdf=5;iwantthis123jasd'
result = re.search('asdf=5;(.*)123jasd', s)
print(result.group(1))
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)
Related Topics
Curly Braces in "New" Expression? (E.G. "New Myclass() { ... }")
Java.Lang.Noclassdeffounderror: Com.Google.Ads.Adview
Serializing and De-Serializing Android.Graphics.Bitmap in Java
Urlconnection Getcontentlength() Is Returning a Negative Value
Efficiency of Searching Using Wherearraycontains
Why Does Bitmapfactory.Decodebytearray Return Null
How to Update One Field from All Documents Using Pojo in Firestore
I Cant See Any Firebaserecycleradapter Items on My Layout
Connectiontimeout Versus Sockettimeout
What Is the Simplest Way to Reverse an Arraylist
Choosing Java VS Python on Google App Engine
Serializing with Jackson (JSON) - Getting "No Serializer Found"
Why Is Double.Min_Value in Not Negative
How Does the Fetchmode Work in Spring Data JPA