How to extract a substring using regex in java - java

I have the following string :
String xmlnode = "<firstname id="{$person.id}"> {$person.firstname} </firstname>";
How can I write a regex to extract the data inside the {$STRING_I_WANT}
The part I need is without {$} how can I achieve that?

You can use this regex \{\$(.*?)\} with pattern like this :
String xmlnode = "<firstname id=\"{$person.id}\"> {$person.firstname} </firstname>";
Pattern pattern = Pattern.compile("\\{\\$(.*?)\\}");
Matcher matcher = pattern.matcher(xmlnode);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
Note : you have to escape each character { $ } with \ because each one is special character in regex.
Outputs
person.id
person.firstname

Related

How parse key-value with regex

i use Kotlin \ Java for parse some string.
My regex:
\[\'(.*?)[\]]=\'(.*?)(?!\,)[\']
text for parse:
someArray1['key1'] = 'value1', someArray2['key2'] = 'value2', ignoreText=ignore, some['key3'] = 'value3', ignoreMe['ignore']=ignore, some['key4'] = 'value4'..
i need result:
key1=value1
key2=value2
key3=value3
key4=value4
Thanks for help
Another regex for you
\['(\w+)'\]\s+(=)\s+'(\w+)'
Regex101 Demo Fiddle
Java test code
String str = "someArray1['key1'] = 'value1', someArray2['key2'] = 'value2', ignoreText=ignore, some['key3'] = 'value3', ignoreMe['ignore']=ignore, some['key4'] = 'value4'..";
String regex = "\\['(\\w+)'\\]\\s+(=)\\s+'(\\w+)'";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1) + matcher.group(2) + matcher.group(3));
}
Test result:
key1=value1
key2=value2
key3=value3
key4=value4
A few notes about the pattern that you tried
In your pattern you are not matching the spaces around the equals sign.
Also note that this part (?!\,)[\'] will always work as it says that it asserts not a comma to the right, and then matches a single quote.
You don't have to escape the \' and the single characters do not have to be in a character class.
You can use a pattern with a negated character class to capture the values between the single quotes to prevent .*? matching too much as the dot can match any character.
You might write the pattern as
\['([^']*)'\]\h+=\h+'([^']*)'
The pattern matches:
\[' Match ['
( Capture group 1
[^']* Match optional chars other than '
) Close group 1
'\] Match ']
\h+=\h+ Match an equals sign between 1 or more horizontal whitespace characters
'([^']*)' Capture group 2 which has the same pattern as group 1
Regex demo | Java demo
Example
String regex = "\\['([^']*)'\\]\\h+=\\h+'([^']*)'";
String string = "someArray1['key1'] = 'value1', someArray2['key2'] = 'value2', ignoreText=ignore, some['key3'] = 'value3', ignoreMe['ignore']=ignore, some['key4'] = 'value4'..";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println(matcher.group(1) + "=" + matcher.group(2));
}
Output
key1=value1
key2=value2
key3=value3
key4=value4

Java Regex : Extract a specific pattern from a string "I_INSERT_TO_TOPIC_345674_123456_4.json"

I want to extract only "_123456_4" from this string using java Regex.
I_INSERT_TO_TOPIC_345674_123456_4.json
I have tried
Pattern.compile("(_([^_]*_[^_]))") and Pattern.compile("_" + "([^[0-9]]*)" + "_[0-9]") but these do not work.
If you want to get 2 group of digits just before .json then you can use regex group to find the required match. You can modify the pattern as per your requirement.
Pattern p = Pattern.compile("(_\\d+_\\d+)\\.json");
Matcher matcher = p.matcher(s);
if (matcher.find()) {
String group = matcher.group(1);
}
【\_[0-9]\*\_[0-9]\*(?=\\.)】
You can try to see if this works

extract a set of a characters between some characters

I have a string email = John.Mcgee.r2d2#hitachi.com
How can I write a java code using regex to bring just the r2d2?
I used this but got an error on eclipse
String email = John.Mcgee.r2d2#hitachi.com
Pattern pattern = Pattern.compile(".(.*)\#");
Matcher matcher = patter.matcher
for (Strimatcher.find()){
System.out.println(matcher.group(1));
}
To match after the last dot in a potential sequence of multiple dots request that the sequence that you capture does not contain a dot:
(?<=[.])([^.]*)(?=#)
(?<=[.]) means "preceded by a single dot"
(?=#) means "followed by # sign"
Note that since dot . is a metacharacter, it needs to be escaped either with \ (doubled for Java string literal) or with square brackets around it.
Demo.
Not sure if your posting the right code. I'll rewrite it based on what it should look like though:
String email = John.Mcgee.r2d2#hitachi.com
Pattern pattern = Pattern.compile(".(.*)\#");
Matcher matcher = pattern.matcher(email);
int count = 0;
while(matcher.find()) {
count++;
System.out.println(matcher.group(count));
}
but I think you just want something like this:
String email = John.Mcgee.r2d2#hitachi.com
Pattern pattern = Pattern.compile(".(.*)\#");
Matcher matcher = pattern.matcher(email);
if(matcher.find()){
System.out.println(matcher.group(1));
}
No need to Pattern you just need replaceAll with this regex .*\.([^\.]+)#.* which mean get the group ([^\.]+) (match one or more character except a dot) which is between dot \. and #
email = email.replaceAll(".*\\.([^\\.]+)#.*", "$1");
Output
r2d2
regex demo
If you want to go with Pattern then you have to use this regex \\.([^\\.]+)# :
String email = "John.Mcgee.r2d2#hitachi.com";
Pattern pattern = Pattern.compile("\\.([^\\.]+)#");
Matcher matcher = pattern.matcher(email);
if (matcher.find()) {
System.out.println(matcher.group(1));// Output : r2d2
}
Another solution you can use split :
String[] split = email.replaceAll("#.*", "").split("\\.");
email = split[split.length - 1];// Output : r2d2
Note :
Strings in java should be between double quotes "John.Mcgee.r2d2#hitachi.com"
You don't need to escape # in Java, but you have to escape the dot with double slash \\.
There are no syntax for a for loop like you do for (Strimatcher.find()){, maybe you mean while

I want to extracting css image path by Java Pattern expression

All , I want to write a pattern regex to extract the: "/images/colorbox/ie6/borderBottomRight.png" from cssContent=".cboxIE6 #cboxBottomRight{background:url(../images/colorbox/ie6/borderBottomRight.png);}"
Who can write a pattern regex for me? Thanks a lot.
My regex can't work as:
Pattern pattern = Pattern.compile("[.*]*/:url/(/././/(.+?)/)/;[.*]*");
Matcher matcher = pattern.matcher(cssContent);
if(matcher.find()){
System.out.println(matcher.group(0));
}
Pattern pattern = Pattern.compile(":url\\(\\.\\.([^)]+)\\)");
Matcher matcher = pattern.matcher(cssContent);
if(matcher.find()){
System.out.println(matcher.group(1));
}
The regex used to match is (quoted and without \ escaped)
":url\(\.\.([^)]+)\)"
which looks for :url(.. followed by [^)] anything that's not a closing ) bracket + one or more times; finally followed by the closing ) bracket. The group () captured is available at group(1) whereas group(0) would give you the complete string that matched i.e. from :url to the closing ).
The biggest error you were making was using "/" to escape your literal characters. You need to use "\", and annoyingly, in a java string "\" must be escaped with "\", so the total escape sequence is "\\". Then, you have matcher.group(0), which matches the entire pattern. You needmatcher.group(1)` to match the first (and only) group in your regex, which contains your string of interest. Here's the corrected code:
String cssContent = "cssContent=\".cboxIE6 #cboxBottomRight{background:url(../images/colorbox/ie6/borderBottomRight.png);}\"";
String regex = ".*?:url\\(\\.\\.(.+?)\\);[.*]*";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(cssContent);
if(matcher.find()){
System.out.println(matcher.group(1));
}

Java regex to get part number

I have HTML that I need to extract a part number from, the HTML looks like:
javascript:selectItem('ABC123 1', '.....
I need to get the ABC123 from the above.
My code snippet:
Patterp p = Pattern.Compile("?????");
Matcher m = p.matcher(html);
if(m.find())
partNumber = m.group(1).trim();
BTW, in the pattern, how do I escape for the character (
I now for quotes I do \"
thanks allot!
You escape ( by putting a \ before it. Because it's in a String, you need to escape the \ so the sequence is \\(. This should parse that snippet:
Pattern p = Pattern.compile("javascript:selectItem\\('(\\w+)");
Matcher m = p.matcher(html);
if (m.find()) {
String partNumber = m.group(1);
}
I've assumed the part number is one or more word characters (meaning digits, letters or underscore).
You could use this:
Pattern regex = Pattern.compile("(?<=selectItem\\(')\\S*",Pattern.CASE_INSENSITIVE);
Matcher regexMatcher = regex.matcher(subjectString);
if (regexMatcher.find()) {
ResultString = regexMatcher.group(1);
}

Categories

Resources