Split javaString into substring of characters and numeric value - java

My Requirement is like , split the given string into sub strings of charter and numeric values.
And the input value will always begin charter only.
Input : String strValue = "ABCD12345";
Out put:
A1 = ABCD
A2 = 12345

You need to use lookaround assertions.
String s = "ABCD12345";
String parts[] = s.split("(?<=[A-Za-z])(?=\\d)");
System.out.println(Arrays.toString(parts));
(?<=[A-Za-z])(?=\\d) regex would match the boundary which exists between alphabets and digits. Splitting according to the matched boundary will ive you the desired output.
Output:
[ABCD, 12345]

There's probably some mistakes and probably not the most efficient here but here's a vauge shot. Feedback welcome of course.
var char;
var = num;
str = "somestring23498";
for(i=1:1=str.length - 1;i++){
if ((typeof str[i] + 1) == "string"){
char = char + str[i];
} else if((typeof str[i] + 1) == "integer"){
num = num + str[i];
}
}

You can alternatively create a char buffer with space or a delimeter between char boundary.
String str = "ABCD1234cda209ad";
char[] chars = new char[2*str.length()];
int i=0;
boolean isPrevcharANum = false;
for(char c:str.toCharArray()){
if(Character.isDigit(c)){
if(!isPrevcharANum){
chars[++i] = ' ';
}
isPrevcharANum = true;
}else{
if(isPrevcharANum){
chars[++i] = ' ';
isPrevcharANum = false;
}
}
chars[++i] = c;
}
System.out.println("Output : "+new String(chars));
Output : ABCD 1234 cda 209 ad

again with regex but shorter
String parts[] = s.split("(?<=\\D)(?=\\d)"); // [ABCD, 12345]
Internally, split() uses a Pattern to find the place where to split. The first part in braces is a positive lookbehind.
\D is the oposite to \d and means each character but a digit.
The second part in braces is a positive lookahead \d which means here must be a digit.
So a split is made when to the left of the position is any character but a digit and at the same time there is a digit in this position.

Related

Count all sequences of characters

I have an Array of Characters ' A ' and ' B '
Whats a smart way to count the ' Runs '
example :
AABBBAAAABBAB
should be 6 because there are 6 runs as seen below.
1 2 3 4 5 6
AA BBB AAAA BB A B
tried something like:
if (!(sortedRuns.get(i) == sortedRuns.get(i+1))) {
runsAmount++;
}
but obviously run into 'out of bound problems'
Issue
e.g char array A A B B C C
array positions 0 1 2 3 4 5
when you reach at the position 5 then sortedRuns.get(i+1) mean 5+1=6 which doesn't exist hence the exception
Solution
1.) Traverse the array
2.) Increment run if char changes and assign new char to temp char
String s="AABBBAAAABBAB";
int run=1;
// fetch first char
char temp=s.charAt(0);
// traverse char array
for (char ch : s.toCharArray()) {
// assign value of new char to temp and increment run
// when value of char changes
if (ch!=temp) {
run++;
temp=ch;
}
}
System.out.println(run);
Output:
6
I would use an auxiliar variable to save the last character. Then increment when a new character is different from my auxiliar variable, then update this variable. Easy and not out of bounds exceptions.
u can try this easy one-liner:
public static void main(String[] args) {
String s="AABBBAAAABBAB";
int charCount = s.length() - s.replaceAll("A", "").length();
System.out.println(charCount);
System.out.println(s);
}
You could use a regular expression.
/(.)\1*/ gives 6 matches.
(.) matches any character.
\1* then matches as many of the first character as possible.
Java Example:
final String regex = "(.)\\1*";
final String string = "AABBBAAAABBAB";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
int count = 0;
while (matcher.find()) {
count ++;
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
System.out.println("Match count: " + count);
This has the advantage of working with any character string.

Regular Expressions - match a string containing "+" and "-"

I have a string, say 1+++-3--+++++2 that includes + and - sign.
What I want to do is to represent the + and - part with + or - sign.
If there are an odd number of - sign in the string, I will replace it with -, and + if that is an even number. How can I do that using regex?
For example, I have a math expression, say 1+-+-2-+--+3. It will be replaced by 1+2-3
You can create an array of the operators and use a for loop to count all occurrences of one character. For example:
String expression = "1+++-3--+++++2";
String[] str = expression.split("[0-9]+");
for(op : str) {
int count = 0;
for(int i =0; i < str.length(); i++)
if(op.charAt(i) == '-')
count++;
if(count % 2 == 0) {
op = "-";
}
else {
op = "+";
}
}
After assigning the modified one-character operators in str[], it should be relatively simple to write the new expression.
based on the assumption that the format will be from that calculator example.
//assumed format for input: <any number><any number of `-` and/or `+`><any number>
// 1++---+22+--1 will be 1-22+1
String input = "1++---+22+--1";
for (String s : input.split("[^-+]+");) {
s = s.trim();
if (!"".equals(s)) {
String newStr = s.matches("[+]*-([+]*-[+]*-)*[+]*") ? "-" : "+";
input = input.replace(s, newStr);
}
}
System.out.println(input);

Splitting an input currency into a string and an int

In a program where currency is input in the form £2 or 10p, for example, is there a method to split this into two variables in the form
currencyType = £
currencyValue = 2
or
currencyType = p
currencyValue = 10
where currencyType is a string and currencyValue is an int?
An idea for a solution without regular expressions, although I'd prefer one of those:
String entry = "€2.73";
StringBuilder currency = new StringBuilder();
StringBuilder value = new StringBuilder();
for (char c : entry.toCharArray()) {
if (Character.isDigit(c) || c == '.' || c == ',') {
value.append(c);
} else {
currency.append(c);
}
}
System.out.println("Value = " + value + " Currency = " + currency);
Use patten and matcher classes like below. \d+ matches one or more digits where \D+ matches one or more non-digit characters.
String s1 = "£2";
Matcher m = Pattern.compile("(\\D+)|(\\d+)").matcher(s1);
while(m.find())
{
if (m.group(1) != null)
System.out.println("Currency Type: " + m.group(1));
if (m.group(2) != null)
System.out.println("Currency Value: " + m.group(2));
}
Output:
Currency Type: £
Currency Value: 2
OR
Use this regex, if you want to deal also with the decimal value.
Pattern.compile("(\\D+)|(\\d+(?:\\.\\d+)?)");
DEMO
You can use this regular expression to get your result: "(.*?)([\\d,]*)(.*?)"
This will split the input into three groups:
1) Leading currency token
2) Value token (can contain a ',', in the string version you can replaceAll ',' with '' and then convert to integer)
3) Trailing currency token
By looking at the groups from the regex, you can figure out if the leading or trailing currency is present and then get the value from the second group. You can write the code yourself by looking up usage for java regex.
You can input your value as a string, split it normally with the split function and assign each value to its own string. Then convert the string to an integer.
int currencyValue = Integer.parseInt(array[0]);
String currencyType = array[1];
array[] is the array that you split the string into.
String input = user_input.nextLine();
char[] array = input.toCharArray();
for(int i = 0; i < input.length(); i++) {
if (Character.isLetter(array[i])){
//use .split based on the output of the if statement
}
}

Java: Match with Regex and replace every character

I have a string, for ex:
There exists a word *random*.
random will be a random word.
How can I use a regular expression to replace every character of random with * and have this result:
There exists a word ********.
So the * replaces every character, in this case 6 characters.
Notice that I am after to replace only the word random, not the surroundings *.
So far I have:
str.replaceAll("(\\*)[^.]*(\\*)", "\\*");
But it replaces *random* with *, instead of the desired ******** (total of 8).
Any help, really appreciated...
If you have just a single word like that: -
As far as current example is concerned, if you are having just a single word like that, then you can save yourself from regex, by using some String class methods: -
String str = "There exists a word *random*.";
int index1 = str.indexOf("*");
int index2 = str.indexOf("*", index1 + 1);
int length = index2 - index1 - 1; // Get length of `random`
StringBuilder builder = new StringBuilder();
// Append part till start of "random"
builder.append(str.substring(0, index1 + 1));
// Append * of length "random".length()
for (int i = 0; i < length; i++) {
builder.append("*");
}
// Append part after "random"
builder.append(str.substring(index2));
str = builder.toString();
If you can have multiple words like that: -
For that, here's a regex solution (This is where it starts getting a little complex): -
String str = "There exists a word *random*.";
str = str.replaceAll("(?<! ).(?!([^*]*[*][^*]*[*])*[^*]*$)", "*");
System.out.println(str);
The above pattern replaces all the characters that is not followed by string containing even numbers of * till the end, with a *.
Whichever is appropriate for you, you can use.
I'll add an explanation of the above regex: -
(?<! ) // Not preceded by a space - To avoid replacing first `*`
. // Match any character
(?! // Not Followed by (Following pattern matches any string containing even number of stars. Hence negative look-ahead
[^*]* // 0 or more Non-Star character
[*] // A single `star`
[^*]* // 0 or more Non-star character
[*] // A single `star`
)* // 0 or more repetition of the previous pattern.
[^*]*$ // 0 or more non-star character till the end.
Now the above pattern will match only those words, which are inside a pair of stars. Provided you don't have any unbalanced stars.
You can extract the word between * and do a replaceAll characters with * on it.
import java.util.regex.*;
String txt = "There exists a word *random*.";
// extract the word
Matcher m = Pattern.compile("[*](.*?)[*]").matcher(txt);
if (m.find()) {
// group(0): *random*
// group(1): random
System.out.println("->> " + m.group(0));
txt = txt.replace(m.group(0), m.group(1).replaceAll(".", "*"));
}
System.out.println("-> " + txt);
You can see it on ideone: http://ideone.com/VZ7uMT
try
String s = "There exists a word *random*.";
s = s.replaceAll("\\*.+\\*", s.replaceAll(".*(\\*.+\\*).*", "$1").replaceAll(".", "*"));
System.out.println(s);
output
There exists a word ********.
public static void main(String[] args) {
String str = "There exists a word *random*.";
Pattern p = Pattern.compile("(\\*)[^.]*(\\*)");
java.util.regex.Matcher m = p.matcher(str);
String s = "";
if (m.find())
s = m.group();
int index = str.indexOf(s);
String copy = str;
str = str.substring(0, index);
for (int i = index; i < index + s.length(); i++) {
str = str + "*";
}
str = str + copy.substring(index + s.length(), copy.length());
System.out.println(str);
}

Java Regular Expression to Replace all spaces surrounded by digits by a character

I want to replace the spaces surrounded by digits or numbers by -.
so if my string is I am Bob 12 12 This should get transformed to I am Bob 12-12.
Please suggest some possible answers using Java.
str = str.replaceAll("(?<=\\d)\\s(?=\\d)", "-");
This looks for a space (\\s) preceded by a digit ((?<=\\d)) and followed by a digit ((?=\\d)), and replaces that space with a dash.
If you want to replace groups of consecutive spaces, change \\s to \\s+.
A non-regexp solution:
String in = "I am Bob 12 12";
int last = in.length()-1;
StringBuilder resultBuilder = new StringBuilder();
resultBuilder.append(in.charAt(0)); // assumes, the String has at least one char
for (int i = 1; i < last-1; i++) {
if (in.charAt(i) == ' ' &&
Character.isDigit(in.charAt(i-1)) &&
Character.isDigit(in.charAt(i+1))) {
resultBuilder.append('-');
} else {
resultBuilder.append(in.charAt(i));
}
}
resultBuilder.append(in.charAt(last));
// System.out.println(resultBuilder);

Categories