How to remove alpha characters from timestamp using RegEx? [duplicate] - java

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I have timestamps in a field and would like to remove the 'T' and the 'Z' in the value. An example value is 2019-11-01T14:47:43Z. I would like to use a RegEx to solve this problem. I plan to use this in Java.

You can use Java's String.replaceAll() function to remove values with regex. The regular expression [a-zA-Z] will match any one letter; replacing it with an empty string will remove it entirely.
String ts = "2019-11-01T14:47:43Z";
System.out.println(ts.replaceAll("[a-zA-Z]", ""));
2019-11-0114:47:43
Demo

Related

Replacing Regular expression matches in Java [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 4 years ago.
I want to replace &sp; in the string below with Z.
Input text : ABCD&sp;EF&p;GHIJ&bsp;KL
Output text : ABCDZEFZGHIZKL
Can anyone tell me how to replace the every instance of &\D+; using java regular expression?
I am using /(&\D+;)?/ but it doesn't work.
Use String#replaceAll.
You also should use the ? modificator to +:
String str = "ABCD&sp;EF&p;GHIJ&bsp;KL";
String regex = "&\\D+?;";
System.out.println (str.replaceAll(regex,"Z"));
This should work
Match the initial &, then all characters that are not the tailing ;, then that tailing ; like so: &[^;]+; If not matching numbers (as suggested by your example with \D) is a requirement, add the numbers to the negated character set: [^;0-9] To make it replace all occurrences, add the global flag g. The site regexr.com is a handy tool to create regexes.
Edit: Sorry, I initially read your question wrong.

Split a mathematical function containing using signs in java [duplicate]

This question already has answers here:
How to Split a mathematical expression on operators as delimiters, while keeping them in the result?
(5 answers)
Closed 4 years ago.
I want to split a mathematical function by the sign of the variables in it like this :
input--> x-5y+3z=10
output--> [x,-5y,+3z,=10]
this code does not work in the way i want :
String function = "x-5y+3z=10";
String split = function.split("=|-|\\+");
the output of the array is :
[x,5y,3z,10]
so what is the correct regex for this ?
The "problem" using split is that the delimiter used will be removed, because it'll takt the parts that are between this delimiter, you need a pattern that is non-capturing or with a simple lookahead : match something wich is before something else
The pattern (?=[-+=]) would work, it'll take the part that starts with a -+= symbol without removing it :
String function = "x-5y+3z=10";
String[] split = function.split("(?=[-+=])");
System.out.println(Arrays.toString(split)); //[x, -5y, +3z, =10]
Some doc on Lookahead

Escape special character ^ in a regular expression in Java [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 6 years ago.
I want to split a string in Java with the following format:
"value^1" so that I get the initial part, "value" in a string.
I wanted to use split instead of substring, so I tried this:
string.split("^")[0]
but unfortunately ^ is a special character and I guess it has to be escaped. I tried split("\^") but no luck.
Anybody knows how to achieve this using split?
Thanks!
Escape the escape: split("\\^"). Or split(Pattern.quote("^")).
But since you only want the first part of it, there's no need to use split:
int pos = string.indexOf('^');
String firstPart = (pos != -1) ? string.substring(0, pos) : string;

Stripping specific chars from beginning/ending of a string [duplicate]

This question already has answers here:
Regex to trim hyphens from start and end of a string
(2 answers)
Closed 7 years ago.
Given a word-string in Java, I want to strip off from beginning and from end, exactly these specified set of characters:
[?:!.,;'\"«»]
as many times as they appear.
For instance, «Be!!» should become just Be, "Here!!!" should become Here, «I should become I.
Can anyone provide a correct way to do this?
Use an anchored regex in string.replaceAll function.
string.replaceAll("^[?:!.,;'\"«»]+|[?:!.,;'\"«»]+$", "");
DEMO

Regex, trim multiple characters? [duplicate]

This question already has answers here:
Removing repeated characters in String
(4 answers)
Closed 8 years ago.
Lets say I have a string:
tttteeeeeeessssssttttttt
Using the power of regex, how can that string be turned into:
test
At first look it seems easy to do, but the current code (not regex) I have for it is not behaving well and im pretty sure regex is the way to go.
You can use:
str = str.replaceAll("([A-Za-z])\\1+", "$1");
RegEx Demo
Use string.replaceAll function.
strng.replaceAll("(.)\\1+", "$1");
The above regex captures the first character in the sequence of same characters and matches all the following one or more characters (which must be same as the one inside the capturing group) . Replacing those characters with the character inside group index 1 will give you the desired output.
Example:
System.out.println("tttteeeeeeessssssttttttt".replaceAll("(.)\\1+","$1" ));
Output:
test
(.)(?=\1)
Try this.Replace by empty string.See demo.
https://regex101.com/r/tX2bH4/41
str = str.replaceAll("(.)(?=\\1)", "");

Categories