I would like to replace all matching substrings of a string with a hashing of them.
Lets say I have a string like this
String myString = "This is a A1B4F string with some 342BF matches FFABC that should be replaced.";
And now I would like to replace all the matching strings to regex (for example here "([a-fA-F\d]{5})" ) with their hashed value.
Assume that there is a sting method that gets as a parameter the substring and returns its sha1 value
public static String giveMeTheSha1Of(String myClearText){
return ....; (the sha1 value of the string)
}
How can I find all the matching substrings, and replace them with their hash?
Thank you Rohit Jain and Marko Topolnik. With your comments I found what I was searching for.
public static String replace5CharHex(String input){
String REGEX = "([a-fA-F\\d]{5})";
String tmpSubstring = "";
Pattern p = Pattern.compile(REGEX);
Matcher m = p.matcher(input);
StringBuffer sb = new StringBuffer();
while (m.find()) {
tmpSubstring = hashManager.createNewHash(m.group());
m.appendReplacement(sb, tmpSubstring);
}
m.appendTail(sb);
return sb.toString();
}
Related
This question already has answers here:
How to increment string variable? [closed]
(4 answers)
Closed 6 years ago.
Is there any Java solution of replacing a digit in a String other than getting the digit using a matcher, increment it by one and replace it?
"REPEAT_FOR_4" will return "REPEAT_FOR_5"
"REPEAT_FOR_10" will return "REPEAT_FOR_11"
I would like to do it in one line with regex and replace, not by recomposing the String as "REPEAT_FOR_" and add the number after incrementation.
Thank you!
Later edit: I would like to know how to replace a number with the following one in a String.
I didn't use regex but here is the solution in one line. Considering your string remains the same.
public String getIncrementedString (String str){
return ("REPEAT_FOR_" + (Character.getNumericValue(str.charAt(11))+1));
}
Yes of course it's possible. using regex Pattern and Matcher, here's what you will need to do:
String str = "REPEAT_FOR_4";
Pattern p = Pattern.compile("([0-9]+)");
Matcher m = p.matcher(str);
StringBuffer s = new StringBuffer();
while (m.find())
m.appendReplacement(s, String.valueOf(1+ Integer.parseInt(m.group(1))));
String updated = s.toString();
System.out.println(updated);
This is a working Example that returns REPEAT_FOR_5 as output.
You can try this.
String ss = "REPEAT_FOR_4";
int vd = Integer.valueOf(ss.substring(ss.length() - 1));
String nss = ss.replaceAll("\\d",String.valueOf(vd+1));
System.out.println(nss);
output:
REPEAT_FOR_5
with regex: if the digit is not at the end of the string.
String ss = "REPEAT_5_FOR_ME";
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(ss);
m.find();
String strb = m.group();
int vd = Integer.valueOf(strb);
String nss = ss.replaceAll("\\d",String.valueOf(vd+1));
System.out.println(nss);
output:
REPEAT_6_FOR_ME
Base on the issue raised in the comments comments i think this solution with regex will help.
public static String convStr(String str){
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(str);
m.find();
String strb = m.group();
int vd = Integer.valueOf(strb);
return str.replaceAll("\\d",String.valueOf(vd+1));
}
I am using regex in java to get a specific output from a list of rooms at my University.
A outtake from the list looks like this:
(A55:G260) Laboratorium 260
(A55:G292) Grupperom 292
(A55:G316) Grupperom 316
(A55:G366) Grupperom 366
(HDS:FLØYEN) Fløyen (appendix)
(ODO:PC-STUE) Pulpakammeret (PC-stue)
(SALEM:KONF) Konferanserom
I want to get the value that comes between the colon and the parenthesis.
The regex I am using at the moment is:
pattern = Pattern.compile("[:]([A-Za-z0-9ÆØÅæøå-]+)");
matcher = pattern.matcher(room.text());
I've included ÆØÅ, because some of the rooms have Norwegian letters in them.
Unfortunately the regex includes the building code also (e.g. "A55") in the output... Comes out like this:
A55
A55
A55
:G260
:G292
:G316
Any ideas on how to solve this?
The problem is not your regular expression. You need to reference group(1) for the match result.
while (matcher.find()) {
System.out.println(matcher.group(1));
}
However, you may consider using a negated character class instead.
pattern = Pattern.compile(":([^)]+)");
You can try a regex like this :
public static void main(String[] args) {
String s = "(HDS:FLØYEN) Fløyen (appendix)";
// select everything after ":" upto the first ")" and replace the entire regex with the selcted data
System.out.println(s.replaceAll(".*?:(.*?)\\).*", "$1"));
String s1 = "ODO:PC-STUE) Pulpakammeret (PC-stue)";
System.out.println(s1.replaceAll(".*?:(.*?)\\).*", "$1"));
}
O/P :
FLØYEN
PC-STUE
Can try with String Opreations as follows,
String val = "(HDS:FLØYEN) Fløyen (appendix)";
if(val.contains(":")){
String valSub = val.split("\\s")[0];
System.out.println(valSub);
valSub = valSub.substring(1, valSub.length()-1);
String valA = valSub.split(":")[0];
String valB = valSub.split(":")[1];
System.out.println(valA);
System.out.println(valB);
}
Output :
(HDS:FLØYEN)
HDS
FLØYEN
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class test
{
public static void main( String args[] ){
// String to be scanned to find the pattern.
String line = "(HDS:FLØYEN) Fløyen (appendix)";
String pattern = ":([^)]+)";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
while (m.find()) {
System.out.println(m.group(1));
}
}
}
I want to split this String to give my desired output
sinXcos(b+c)
Gives output as
sinX
cos(b+c)
I know how to split a string like
200XY
using
token = 200XY;
String[] mix_token = token.split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
But how can I use something like this on a string like
sinXcos(b+c)
or a String like
sinXcos(b+c)tan(z)
This will work..
public static void main(String[] args) {
String text = "sinXcos(b+c)tan(z)";
String patternString1 = "(sin|cos|tan)(?![a-z])\\(?\\w(\\+\\w)?\\)?";
Pattern pattern = Pattern.compile(patternString1);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
O/P:
sinX
cos(b+c)
tan(z)
2. Input :"sinabc(X+y)cos(b+c)tan(z)";
O/P :
cos(b+c)
tan(z)
Explaination :
S
tring patternString1 = "(sin|cos|tan)(?![a-z])\\(?\\w(\\+\\w)?\\)?";
1. (sin|cos|tan) -->start with (sin or cos or tan)
2. (?:![a-z]) --> negative lookahead. check if the next character is not in between [a to z].
3. \\(?\\w(\\+\\w)?\\)?--> an optional brace followed by an alphabet followed by a "+" and another alphabet.
How split a [0] like words from string using regex pattern.0 can replace any integer number.
I used regex pattern,
private static final String REGEX = "[\\d]";
But it returns string with [.
Spliting Code
Pattern p=Pattern.compile(REGEX);
String items[] = p.split(lure_value_save[0]);
You have to escape the brackets:
String REGEX = "\\[\\d+\\]";
Java doesn't offer an elegant solution to extract the numbers. This is the way to go:
Pattern p = Pattern.compile(REGEX);
String test = "[0],[1],[2]";
Matcher m = p.matcher(test);
List<String> matches = new ArrayList<String>();
while (m.find()) {
matches.add(m.group());
}
I have a string which contains many <xxx> values.
I want to retrive the value inside <>, do some manipulation and re-insert the new value into the string.
What I did is
input = This is <abc_d> a sample <ea1_j> input <lmk_02> string
while(input.matches(".*<.+[\S][^<]>.*"))
{
value = input.substring(input.indexOf("<") + 1, input.indexOf(">"));
//calculate manipulatedValue from value
input = input.replaceFirst("<.+>", manipulatedValue);
}
but after the first iteration, value contains abc_d> a sample <ea1_j> input <lmk_02. I believe indexOf(">") will give the first index of ">". Where did I go wrong?
This is a slightly easier way of accomplishing what you are trying to do:
String input = "This is <abc_d> a sample <ea1_j> input <lmk_02> string";
Matcher matcher = Pattern.compile("<([^>]*)>").matcher(input);
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
matcher.appendReplacement(sb, manipulateValue(matcher.group(1)));
}
matcher.appendTail(sb);
System.out.println(sb.toString());
This is a good use case for the appendReplacement and appendTail idiom:
Pattern p = Pattern.compile("<([^>]+)>");
Matcher m = p.matcher(input);
StringBuffer out = new StringBuffer():
while(m.find()) {
String value = m.group(1);
// calculate manipulatedValue
m.appendReplacement(out, Matcher.quoteReplacement(manipulatedValue));
}
m.appendTail(out);
Try using an escape character \\ to the regex.