regex how to split String without removing that seperator and adding it in seperate way
You need to use look ahead and look behind like following
String string1="Ram-sita-laxman";
System.out.println(Arrays.toString(string1.split("((?<=-)|(?=-))")));
In this the output will be[Ram, -, sita, -, laxman]
Notice that while the delimiter is there but not everything is in quotes cause it can't be unless you add them yourself in the array
Hope this helps.
Java: Split String with Regex without deleting delimiters
String s="Hi, <name> pls visit <url>";
String[] ss = s.split("(?<=> )|(?=<)");
System.out.println(Arrays.toString(ss));
the above codes output:
[Hi, , <name> , pls visit , <url>]
Splitting on regex without removing delimiters
You can use re.findall
with regex .*?[.!\?]
; the lazy quantifier *?
makes sure each pattern matches up to the specific delimiter you want to match on:
import re
s = """You! Are you Tom? I am Danny."""
re.findall('.*?[.!\?]', s)
# ['You!', ' Are you Tom?', ' I am Danny.']
JS string.split() without removing the delimiters
Try this:
- Replace all of the "d" instances into ",d"
- Split by ","
var string = "abcdeabcde";
var newstringreplaced = string.replace(/d/gi, ",d");
var newstring = newstringreplaced.split(",");
return newstring;
Hope this helps.
split string without removal of delimiter in python
You could use re.split
with forward lookahead:
import re
re.split('\s(?=\d\s)',content)
resulting in:
['This', '1 string is very big', '2 i need to split it', '3 into paragraph wise.', '4 But this string', '5 not a formated string.']
This splits on spaces -- but only those which are immediately followed by a digit then another space.
Python split() without removing the delimiter
d = ">"
for line in all_lines:
s = [e+d for e in line.split(d) if e]
Split a String without removing the delimiter in Swift
This method works on CollectionTypes
, rather than String
s, but it should be easy enough to adapt:
extension CollectionType {
func splitAt(@noescape isSplit: Generator.Element throws -> Bool) rethrows -> [SubSequence] {
var p = startIndex
return try indices
.filter { i in try isSplit(self[i]) }
.map { i in
defer { p = i }
return self[p..<i]
} + [suffixFrom(p)]
}
}
extension CollectionType where Generator.Element : Equatable {
func splitAt(splitter: Generator.Element) -> [SubSequence] {
return splitAt { el in el == splitter }
}
}
You could use it like this:
let sentence = "Hello, my name is oisdk. This should split: but only at punctuation!"
let puncSet = Set("!.,:".characters)
sentence
.characters
.splitAt(puncSet.contains)
.map(String.init)
// ["Hello", ", my name is oisdk", ". This should split", ": but only at punctuation", "!"]
Or, this version, which uses a for-loop, and splits after the delimiter:
extension CollectionType {
func splitAt(@noescape isSplit: Generator.Element throws -> Bool) rethrows -> [SubSequence] {
var p = startIndex
var result: [SubSequence] = []
for i in indices where try isSplit(self[i]) {
result.append(self[p...i])
p = i.successor()
}
if p != endIndex { result.append(suffixFrom(p)) }
return result
}
}
extension CollectionType where Generator.Element : Equatable {
func splitAt(splitter: Generator.Element) -> [SubSequence] {
return splitAt { el in el == splitter }
}
}
let sentence = "Hello, my name is oisdk. This should split: but only at punctuation!"
let puncSet = Set("!.,:".characters)
sentence
.characters
.splitAt(puncSet.contains)
.map(String.init)
// ["Hello,", " my name is oisdk.", " This should split:", " but only at punctuation!"]
Or, if you wanted to get the most Swift features into one function (defer
, throws
, a Protocol extension, an evil flatMap
, guard
, and Optionals):
extension CollectionType {
func splitAt(@noescape isSplit: Generator.Element throws -> Bool) rethrows -> [SubSequence] {
var p = startIndex
var result: [SubSequence] = try indices.flatMap { i in
guard try isSplit(self[i]) else { return nil }
defer { p = i.successor() }
return self[p...i]
}
if p != endIndex { result.append(suffixFrom(p)) }
return result
}
}
How to split a string without removing the delimiter?
You need to add \d+ inside your regular expression, like so:
(__label__\d+:)
This also allows you to capture all numericals rather than having to list all possible values...
Python String Split on pattern without removing delimiter
You can also use a look ahead regex:
import re
re.split(r'.(?=123 my)', my_str)
=>
['123 my string is long',
'123 my string is very long',
'123 my string is so long']
Related Topics
Best Way to Work with Large Amounts of CSV Data Quickly
How to Deal with Ruby 2.1.2 Memory Leaks
Regex to Match Hashtags in a Sentence Using Ruby
Initializing Instance Variables in Mixins
In a Sinatra App on Heroku, Session Is Not Shared Across Dynos
Ruby Find and Return Objects in an Array Based on an Attribute
Passing Binding or Arguments to Erb from the Command Line
C1 or C2 Coverage Tool for Ruby
How to Tell or Hint to Rubymine What Type a Local or Instance Variable Is
How to Pass <Arguments> to Irb If I Don't Specify <Programfile>
"Certificate Verify Failed" Error When Installing Ruby Gems on Windows
Actiondispatch::Http::Uploadedfile.Content_Type Not Being Initialized in Rspec Test
Can't Install Rmagick Gem on Ubuntu 13.04
Reversing the Order of an Array in Ruby
How to Print Something Without a New Line in Ruby
Invoke Delayed_Job Capistrano Tasks Only on Specific Servers