Java RegEx: Replace part of source string - java

If the source string contains the pattern, then replace it with something or remove it. One way to do it is to do something like this
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(sourceString);
while(m.find()){
String subStr = m.group().replaceAll('something',""); // remove the pattern sequence
String strPart1 = sourceString.subString(0,m.start());
String strPart2 = sourceString.subString(m.start()+1);
String resultingStr = strPart1+subStr+strPart2;
p.matcher(...);
}
But I want something like this
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(sourceString);
while(m.find()){
m.group.replaceAll(...);// change the group and it is source string is automatically updated
}
Is this possible?
Thanks

// change the group and it is source string is automatically updated
There is no way what so ever to change any string in Java, so what you're asking for is impossible.
To remove or replace a pattern with a string can be achieved with a call like
someString = someString.replaceAll(toReplace, replacement);
To transform the matched substring, as seems to be indicated by your line
m.group().replaceAll("something","");
the best solution is probably to use
A StringBuffer for the result
Matcher.appendReplacement and Matcher.appendTail.
Example:
String regex = "ipsum";
String sourceString = "lorem ipsum dolor sit";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(sourceString);
StringBuffer sb = new StringBuffer();
while (m.find()) {
// For example: transform match to upper case
String replacement = m.group().toUpperCase();
m.appendReplacement(sb, replacement);
}
m.appendTail(sb);
sourceString = sb.toString();
System.out.println(sourceString); // "lorem IPSUM dolor sit"

Assuming you want to replace all occurences of a certain pattern, try this:
String source = "aabbaabbaabbaa";
String result = source.replaceAll("aa", "xx"); //results in xxbbxxbbxxbbxx
Removing the pattern would then be:
String result = source.replaceAll("aa", ""); //results in bbbbbb

Related

Get a particular string from a data using regular expression

I am trying to get particular string from the data below.It is too long am here with sharing sample data. From this I have to get the 'france24Id=7GHYUFGty6fdGFHyy56'
am not that much familier with regex.
How can I retreive the string 'france24Id=7GHYUFGty6fdGFHyy56' from above data?
I tried splitting the data using ',' but it is not an effective way.That's why I choose regex.
2016-07-29 12:08:46,260 s=xGuide, [xre-10-pipe#6da05f7a[,connection=WebSocketConnectionWrapper[/1.8.9]]] INFO c=c.ore., - onConnect event payload={minimumVersion='0', applicationName='shell', fetl='555', authenticationToken='6y777', sessionAuthToken='', sessionGUID='null', connectURL='http://section?ruleName=Default', partnerId='hp', nativeDimensions=null, appParams={heartbeatRequest=1, france24Id=7GHYUFGty6fdGFHyy56, service=false, networkBuffer={min=150, max=150, step=0}}, deviceCaps={platform=Mac, receiverType=Native, revisions={protocol=1, auth=1, video=1}, pixelDimensions=[1280, 720]}, forceSource=null, reconnect=false, currentCommandIndex=0, reconnectReason=7, authService=9}
You can get what you want with (france\d+Id)=([a-zA-Z0-9]+),. This will grab your string and dump the two parts of it into platform-appropriate capture group variables (for instance, in Perl, $1 and $2 respectively).
In Java, your code would look a little like this:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public String matchID(String data) {
Pattern r = new Pattern("(france\\d+Id)=([a-zA-Z0-9]+),");
Matcher m = r.matcher(data);
return m.group(2);
}
public static void main(String[] args) {
String str = "2016-07-29 12:08:46,260 s=xGuide, [xre-10-pipe#6da05f7a[,connection=WebSocketConnectionWrapper[/1.8.9]]] INFO c=c.ore., - onConnect event payload={minimumVersion='0', applicationName='shell', fetl='555', authenticationToken='6y777', sessionAuthToken='', sessionGUID='null', connectURL='http://section?ruleName=Default', partnerId='hp', nativeDimensions=null, appParams={heartbeatRequest=1, france24Id=7GHYUFGty6fdGFHyy56, service=false, networkBuffer={min=150, max=150, step=0}}, deviceCaps={platform=Mac, receiverType=Native, revisions={protocol=1, auth=1, video=1}, pixelDimensions=[1280, 720]}, forceSource=null, reconnect=false, currentCommandIndex=0, reconnectReason=7, authService=9}";
String regex = ".*(france24Id=[\\d|\\w]*),.*";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
if(matcher.matches()){
System.out.println(matcher.group(1));
}
}
You can use Pattern and Matcher classes in Java.
String data = "2016-07-29 12:08:46,260 s=xGuide, [xre-10-pipe#6da05f7a[,connection=WebSocketConnectionWrapper[/1.8.9]]] INFO c=c.ore., - onConnect event payload={minimumVersion='0', applicationName='shell', fetl='555', authenticationToken='6y777', sessionAuthToken='', sessionGUID='null', connectURL='http://section?ruleName=Default', partnerId='hp', nativeDimensions=null, appParams={heartbeatRequest=1, france24Id=7GHYUFGty6fdGFHyy56, service=false, networkBuffer={min=150, max=150, step=0}}, deviceCaps={platform=Mac, receiverType=Native, revisions={protocol=1, auth=1, video=1}, pixelDimensions=[1280, 720]}, forceSource=null, reconnect=false, currentCommandIndex=0, reconnectReason=7, authService=9}";
String regex1 = "france24Id=[a-zA-Z0-9]+"; //this matches france24Id=7GHYUFGty6fdGFHyy56
String regex2 = "(?<=france24Id=)[a-zA-Z0-9]+"; //this matches 7GHYUFGty6fdGFHyy56 or whatever after "france24Id=" and before ','
Pattern pattern1 = Pattern.compile(regex1);
Pattern pattern2 = Pattern.compile(regex2);
Matcher matcher1 = pattern1.matcher(data);
Matcher matcher2 = pattern2.matcher(data);
String result1, result2;
if(matcher1.find())
result1 = matcher1.group(); //if match is found, result1 should contain "france24Id=7GHYUFGty6fdGFHyy56"
if(matcher2.find())
result2 = matcher2.group(); //if match is found, result1 should contain "7GHYUFGty6fdGFHyy56"
You can also try this one:
String str = "france24Id=7GHYUFGty6fdGFHyy56";
Pattern pattern = Pattern.compile("(?<=france24Id=)([a-zA-Z0-9]+)");
Matcher matcher = pattern.matcher(str);
if (matcher.find()) {
System.out.println("ID = " + matcher.group());
}
And the result is:
ID = 7GHYUFGty6fdGFHyy56

Getting specific words from a sentence

I have a sentence which looks like this {Name=Zeus, Address=Something 21} . I want to get only the words after the = (Zeus Something 21), but it's not working for the both of them. That's my code
String line="";
Pattern pattern = Pattern.compile("[=]+([A-Za-z0-9-_]+)");
for (Entity entity : pq.asIterable()) {
String placeInfo=entity.getProperties().toString();
line=placeInfo;
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
result.add(matcher.group());
}
and that's the result i get: =Zeus.
I suppose it's pattern's fault but i don't know what to change... Any ideas?
The placeinfo equals to something that looks like {Name=Something, Address=Something 21} and it's entity properties i'm getting from my datastore.
Your regex and code seem very close to expected result;
String line = "{Name=Zeus, Address=Something 21}";
Pattern pattern = Pattern.compile("[=]+([A-Za-z0-9-_ ]+)"); //added space
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
System.out.println(matcher.group(1)); //did you try group(1)
}
I'm not sure this is the correct approach? What is the return type of entity.getProperties()? If it's a Map or a Properties or a JSON object you are probably best to use an appropriate getter rather than using a regex on the toString().
eg
Map<String, String> properties = entity.getProperties();
String name = properties.get("Name");
String address = properties.get("Address");
Pattern pattern = Pattern.compile("{Name=(.+), Address=(.+)}");
Matcher matcher = pattern.matcher("{Name=Zeus, Address=Something 21}");
if (!matcher.matches()) throw new RuntimeException();
String name = matcher.group(1);
String address = matcher.group(2);

First and second tocen regex

How could I get the first and the second text in "" from the string?
I could do it with indexOf but this is really boring ((
For example I have a String for parse like: "aaa":"bbbbb"perhapsSomeOtherText
And I d like to get aaa and bbbbb with the help of Regex pattern - this will help me to use it in switch statement and will greatly simplify my app/
If all that you have is colon delimited string just split it:
String str = ...; // colon delimited
String[] parts = str.split(":");
Note, that split() receives regex and compilies it every time. To improve performance of your code you can use Pattern as following:
private static Pattern pColonSplitter = Pattern.compile(":");
// now somewhere in your code:
String[] parts = pColonSplitter.split(str);
If however you want to use pattern for matching and extraction of string fragments in more complicated cases, do it like following:
Pattert p = Patter.compile("(\\w+):(\\w+):");
Matcher m = p.matcher(str);
if (m.find()) {
String a = m.group(1);
String b = m.group(2);
}
Pay attention on brackets that define captured group.
Something like this?
Pattern pattern = Pattern.compile("\"([^\"]*)\"");
Matcher matcher = pattern.matcher("\"aaa\":\"bbbbb\"perhapsSomeOtherText");
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Output
aaa
bbbbb
String str = "\"aaa\":\"bbbbb\"perhapsSomeOtherText";
Pattern p = Pattern.compile("\"\\w+\""); // word between ""
Matcher m = p.matcher(str);
while(m.find()){
System.out.println(m.group().replace("\"", ""));
}
output:
aaa
bbbbb
there are several ways to do this
Use StringTokenizer or Scanner with UseDelimiter method

Regexes for modifying beginning and end of line or string

Given this string 1990January20hello.abc I want to apply regexes to get the final string 1990January-20hello.abc
I thought I could do:
String text = "1990January20hello.abc";
Pattern p = Patter.compile("(.*)(January|Jan)(.*)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(text);
while (m.find()){
String val1 = m.group(2);
String val2 = val1.replace("$", "-");
text = text.replace(val1, val2);
}
When I do that seems like in the while loop it does find "January" but val2 and text stay January. what am I doing wrong? Its as if Java doesn't recognize $ for end of line/string. Ideally I want to say val1.replace("(^|$)","-") so I can get 1990-January-20hello.abc as final string. Please help. Thanks for suggestions in advance.
Use this for a pattern:
(\d+)([A-Za-z]+)(\d+)(.*)
The resulting groups and their values are:
Group 1 = 1990,
Group 2 = January,
Group 3 = 20,
Group 4 = the rest of the string.
And then you can append them together and add whatever you want.
String#replace does not use regular expressions so will not match start or end of line characters. You can use one of the replace methods that does use a regular expression such as replaceAll:
String text = "1990January20hello.abc";
Pattern p = Pattern.compile("(.*)(January|Jan)(.*)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(text);
while (m.find()) {
String val1 = m.group(2);
String val2 = val1.replaceAll("$|^", "-");
text = text.replace(val1, val2);
}
Output:
1990-January-20hello.abc
Try
String s = "1990January20hello.abc";
String s2 = s.replaceAll("(Jan|January)(\\d+)", "$1-$2");
System.out.println(s2);
Or using Matcher
String s3 = Pattern
.compile("(Jan|January)(\\d+)", Pattern.CASE_INSENSITIVE)
.matcher(s).replaceAll("$1-$2");
System.out.println(s3);
String does not recognize all regex patterns and thats why my replace was failing. Heres how I solved it:
String text = "1990January20hello.abc";
Pattern p = Patter.compile("(.*)(January|Jan)(.*)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(text);
while (m.find()){
String val1 = m.group(2); //"January"
Pattern p1 = Pattern.compile(^|$);
Matcher m1 = p1.matcher(val1);
String val2 = m1.replaceAll("-"); //"-January-"
text = text.replace(val1, val2); //"1990-January-20hello.abc"
}
Thank you for feedback!

java regular expression find a string add it to array and then replace original

I have a string of text like this:
This is a[WAIT] test.
What I want to do is search the string for a substring that starts with [ and ends with ]
Each one I find I want to add it to an ArrayList and replace substring in original string with a ^
Here is my regex:
String regex_script = "/^\\[\\]$/"; //Match a string which starts with the character [ ending in the character ]
Here is what I have so far:
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile(regex_script); // Create a pattern to match
Matcher m = p.matcher(line); // Create a matcher with an input string
boolean result = m.find();
while(result) {
m.appendReplacement(sb, "^");
result = m.find();
}
m.appendTail(sb); // Add the last segment of input to the new String
how would I got about doing this? Thanks
you can do:
String regex_script = "\\[([^\\]]*)\\]";
String line = "This is a[WAIT] testThis is a[WAIT] test";
StringBuffer sb = new StringBuffer();
List<String> list = new ArrayList<String>(); //use to record
Pattern p = Pattern.compile(regex_script); // Create a pattern to match
Matcher m = p.matcher(line); // Create a matcher with an input string
while (m.find()) {
list.add(m.group(1));
m.appendReplacement(sb, "[^]");
}
m.appendTail(sb); // Add the last segment of input to the new String
System.out.println(sb.toString());
If you are searching a substring, don't use ^ and $. Those are for beginning and at end at a string (not a word) Try:
String regex_script = "/\[.*\]/";

Categories

Resources