How to Split a String Between Letters and Digits (Or Between Digits and Letters)

How to split a string between letters and digits (or between digits and letters)?

You could try to split on (?<=\D)(?=\d)|(?<=\d)(?=\D), like:

str.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");

It matches positions between a number and not-a-number (in any order).

  • (?<=\D)(?=\d) - matches a position between a non-digit (\D) and a digit (\d)
  • (?<=\d)(?=\D) - matches a position between a digit and a non-digit.

How to split String for a digit and letters in java

A simple solution without regular expressions:
Find the index of the first Letter and split the string at this position.

private String[] splitString(String s) {
// returns an OptionalInt with the value of the index of the first Letter
OptionalInt firstLetterIndex = IntStream.range(0, s.length())
.filter(i -> Character.isLetter(s.charAt(i)))
.findFirst();

// Default if there is no letter, only numbers
String numbers = s;
String letters = "";
// if there are letters, split the string at the first letter
if(firstLetterIndex.isPresent()) {
numbers = s.substring(0, firstLetterIndex.getAsInt());
letters = s.substring(firstLetterIndex.getAsInt());
}

return new String[] {numbers, letters};
}

Gives you:

splitString("123abc") 
returns ["123", "abc"]

splitString("123")
returns ["123", ""]

splitString("abc")
returns ["", "abc"]

split a string into letters and digits in c

In your code you write current<= strlen(MMOC_cod) but you have to remember that you should iterate while current is strictly inferior to strlen(MMOC_cod) since you start at index 0.

I prefer to use memcpy when I know the length:

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define BUFF_SIZE 256

int main()
{
char s[BUFF_SIZE];
char warehouse[BUFF_SIZE];
char productNo[BUFF_SIZE];
char qualifiers[BUFF_SIZE];

printf("Hello World enter your MMOC like >ATL1203S14< \n");
scanf("%s", s);
int n = strlen(s);
if (n > BUFF_SIZE)
return 1;
int i = 0;
while (isalpha((unsigned char)s[i])) {
warehouse[i] = s[i];
i++;
}
warehouse[i] = '\0';
int j = 0;
while (isdigit((unsigned char)s[i]))
productNo[j++] = s[i++];
productNo[j] = '\0';
memcpy(qualifiers,&s[i],n-i);
qualifiers[n-i] = '\0';

printf("warehouse: %s\n", warehouse);
printf("product Number: %s\n", productNo);
printf("qualifiers: %s\n", qualifiers);
return 0;
}

Output:

warehouse: ATL
product Number: 1203
qualifiers: S14

NB: If you receive an input of length superior to BUFF_SIZE your program will return.

How to split a string between digits and characters in java

The problem with the logic in your current lookarounds is that \\D matches any non digit character. This does include letters (such as kb), but it also includes things like ., and any other non digit character. Try splitting between numbers and letters only:

String text = "123.45 kb";
String[] parts = text.split("(?<=[A-Za-z])\\s*(?=\\d)|(?<=\\d)\\s*(?=[A-Za-z])");
System.out.println(Arrays.toString(parts));

This prints:

[123.45, kb]

Java - Split String by Number and Letters

You could try this approach:

String formula = "C3H20IO";

//insert "1" in atom-atom boundry
formula = formula.replaceAll("(?<=[A-Z])(?=[A-Z])|(?<=[a-z])(?=[A-Z])|(?<=\\D)$", "1");

//split at letter-digit or digit-letter boundry
String regex = "(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)";
String[] atoms = formula.split(regex);

Output:

atoms: [C, 3, H, 20, I, 1, O, 1]

Now all even even indices (0, 2, 4...) are atoms and odd ones are the associated number:

String[] a = new String[ atoms.length/2 ];
int[] n = new int[ atoms.length/2 ];

for(int i = 0 ; i < a.length ; i++) {
a[i] = atoms[i*2];
n[i] = Integer.parseInt(atoms[i*2+1]);
}

Output:

a: [C, H, I, O]

n: [3, 20, 1, 1]

Splitting letters and numbers in a string in java

Regex and split can help to do that:

public static void main(String[] args) {
String[] c = "path11".split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
System.out.println(Arrays.toString(c));
}

output=

[path, 11]

How to split letter and digit apart of a string in perl?

You can use a pattern like /(\d+)/ to split the string.

The pattern contains a capturing group; as explained in perldoc split:

If the PATTERN contains capturing groups, then for each separator, an additional field is produced for each substring captured by a group (in the order in which the groups are specified, as per backreferences);

Consider:

use strict;
use warnings;
my $string = "A1BB22CCC333DDDD";
my @result = split /(\d+)/, $string;
print "$_\n" for @result;

Yields:

A
1
BB
22
CCC
333
DDDD

The above solution will return a leading empty element if the string does start with a digit. To avoid this, you can adapt the expression as follows:

my @result = grep length, split /(\d+)/, $string;

How to use regex to split a string containing numbers and letters in java

you can do it in this way

final String regex = "([0-9]{1,})([a-zA-Z]{1,}[a-zA-Z0-9]{0,})";
final String string = "123ahaha1234";

final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);

while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}

matcher.group(1) contains the first part and matcher.group(2) contains the second

you can add it to a list/array using these values



Related Topics



Leave a reply



Submit