Splitting a String Containing Special Characters in to Individual Characters

How to split the string if special character in string and keep the string in same python?

For me your task is better served by re.split rather than re.findall, consider that

import re
text = "asdf****adec****awd**wer*"
parts = re.split(r'(\*+)',text)
print(parts)

output

['asdf', '****', 'adec', '****', 'awd', '**', 'wer', '*', '']

Note that I used captured group to inform re.split to keep separators and also there is empty string in parts and it is considered part after last separators, if this is not acceptable then do

parts = [p for p in re.split(r'(\*+)',text) if p]

Note: I used so-called raw-string to make escaping easier, as * has special meaning if you want literal * you must espace it or put in [ and ]. See re docs for discussion of using raw-strings for re needs.

How to split a string with multiple special characters or delimiters into separate fragments using SQL?

If you're using SQL Server 2017+ it provides translate that can help here combined with string_split:

with sample as (
select 'abcd_45dl/beta3,test' StringCol union all
select 'a56d/beta_46ab'
)
select *
from sample
cross apply String_Split(Translate(StringCol,'_/',',,'),',')

How to split a string with special characters `}`, `/`, `-` and `{` in Java

Try to use Pattern.quote to avoid escaping character by character, It will do that free for you :

String[] suburl = mySuburl.split(Pattern.quote("-{name-of-perpetrators"));

Java regex - split string with leading special characters

Split is behaving as expected by splitting off a zero-length string at the start before the first comma.

To fix, first remove all splitting chars from the start:

String[] sArr = s.replaceAll("^([^a-zA-Z]*\\s*)*", "").split("[^a-zA-Z]+\\s*");

Note that I’ve altered the removal regex to trim any sequence of spaces and non-letters from the front.

You don’t need to remove from the tail because split discards empty trailing elements from the result.

How to account for special characters when splitting string in python

You might use a regex with 3 capturing groups, and then get the values of the groups.

^(\w+(?: \w+)*(?: \([^()]*\))?) (\d+|[¼½¾]) (\w+(?: \w+)*)$

Explanation

  • ^ Start of string
  • ( Capture group 1
    • \w+(?: \w+)* Match 1+ word chars, optionally repeated by space and 1+ word chars
    • (?: \([^()]*\))? Optionally match a space and form opening till closing parenthesis
  • ) Close group and match the space
  • (\d+|[¼½¾]) Capture group 2 Match 1+ digits or 1 of the listed ¼½¾ and space
  • (\w+(?: \w+)*) Capture group 3 Match 1+ word chars, and optionally repeat that preceded by space
  • $ End of string

Regex demo | Python demo

Example code

import re

regex = r"(\w+(?: \w+)*(?: \([^()]*\))?) (\d+|[¼½¾]) (\w+(?: \w+)*)"
s = "Some text (just as 1 example) ¼ more words"
match = re.match(regex, s)
if match:
print(match.group(1))
print(match.group(2))
print(match.group(3))

Output

Some text (just as 1 example)
¼
more words

A bit broader pattern that uses .* to match any char except a newline instead of using \w+

^(.*(?:\([^()]*\))?) (\d+|[¼½¾]) (.+)

Regex demo

How to split string with a specific word contains special characters in it

Split based on "\\[DART1]"

String[] result = value.split("\\[DART1]");
  1. The \\ is required to escape the [ otherwise the compiler would think you want to split on anything that's either D, A, R, T or 1.

Then you can access the individual elements by indexing into the result array.

Split String Based On Multiple Special Characters

You can use strsplit

> unlist(strsplit("Vdm@FVDDM_HL#OV_L&28000016", "\\W+"))
[1] "Vdm" "FVDDM_HL" "OV_L" "28000016"
> unlist(strsplit("Vdm@FVDDM_HL#&28000016", "\\W+"))
[1] "Vdm" "FVDDM_HL" "28000016"

\W+ will match for at least one character

Split the String with special character

If you have only bell characters, you can use:

String data="164/165/165"; //the hidden bells are there, generated via echo $'164\a/165\a/165' | pbcopy
System.out.println("length with hidden bells: "+data.length());

String elems [] = data.split("\\a/");
for(String e:elems) {
System.out.println(e);
System.out.println(e.length());
}

output:

length with hidden bells: 13
164
3
165
3
165
3

You can also use the hexadecimal notation: \x07

See: https://www.cisco.com/c/en/us/td/docs/ios/12_4/cfg_fund/command/reference/cfnapph.html

Notes:

If you have different special characters you can create a char class via the hexadecimal values or you can simply use \D+ if you are only interested in extracting digits from the string or \\W+ if your string also contains letters. Also if your / is always preceded by a character you want to discard, then ./ should work fine too.

How to split a string at a specific character but not replace that character?

You can use Lookahead and Lookbehind Zero-Length Assertions.

Example:

// string
$str = "abc;def;ghi{jkl;}if('mno') {pqr;}";
// expression
$pattern = "/(?=[;{\(])/";

$result = preg_split($pattern, $str);

$result is a array like

array (
0 => "abc",
1 => ";def",
2 => ";ghi",
3 => "{jkl",
4 => ";}if",
5 => "('mno') ",
6 => "{pqr",
7 => ";}",
)

How to split String with special character using Java

+ is a reserved character in regular expression and split takes regExp as a parameter. You can escape it by \\+ which will now match +.



Related Topics



Leave a reply



Submit