use java regex,how can i get group value with "( )" string? - java

I have strings:
#Table(name = "T_MEM_MEMBER_ADDRESS1")
#Table( name = "T_MEM_MEMBER_ADDRESS2")
#Table ( name = "T_MEM_MEMBER_ADDRESS3" )
I want to write a regex, which can get the name value,such as :
T_MEM_MEMBER_ADDRESS1
T_MEM_MEMBER_ADDRESS2
T_MEM_MEMBER_ADDRESS3
I write
String regexPattern="...";
Pattern pattern = Pattern.compile(regexPattern);
Matcher matcher = pattern.matcher(input);
boolean matches = matcher.matches();
if (matches){
log.debug(matcher.group(1));
}
but i cannot write the regexPattern..

You can use this regex:
(?<=")(.+)(?=")
In Java:
String regexPattern="(?<=\")(.+)(?=\")";
It uses look-behinds and lookaheads.
Group 1 will contain what you want.

You can use this piece of code:
String input = "#Table(name = \"T_MEM_MEMBER_ADDRESS1\")";
String regexPattern=".*\"(.*)\".*";
Pattern pattern = Pattern.compile(regexPattern);
Matcher matcher = pattern.matcher(input);
boolean matches = matcher.matches();
if (matches){
System.out.println(matcher.group(1));
}
Hope it helps.

Related

How to match regex in java

I want a pattern like this: GJ-16-RS-1234 and I have applied following patterns but they are not working.
My regex patterns are:
String str_tempPattern = "(^[A-Z]{2})\\-([0-9]{2})\\-([A-Z]{1,2})\\-([0-9]{1,4}$)";
String str_tempPattern = "(^[A-Z]{2})-([0-9]{1,2})-([A-Z]{1,2})-([0-9]{1,4})$";
String str_tempPattern = "^[A-Z]{2}\\-[0-9]{1,2}\\-[A-Z]{1,2}\\-[0-9]{1,4}$";
And I am using text watcher to check for any change in the aftertextchange()
Pattern p = Pattern.compile(str_tempPattern, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(s);
if (m.find()){
}
Just set the condition using matches method.
if (string.matches("[A-Z]{2}\\-[0-9]{1,2}\\-[A-Z]{1,2}\\-[0-9]{1,4}"))
{
// Yes it matches
}
else
{
// No it won't
}

how can I get particular string(sub string ) from a string

I have string like
{Action}{RequestId}{Custom_21_addtion}{custom_22_substration}
{Imapact}{assest}{custom_23_multiplication}.
From this I want only those sub string which contains "custom".
For example from above string I want only
{Custom_21_addtion}{custom_22_substration}{custom_23_multiplication}.
How can I get this?
You can use a regular expression, looking from {custom to }. It will look like this:
Pattern pattern = Pattern.compile("\\{custom.*?\\}", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputString);
while (matcher.find()) {
System.out.print(matcher.group());
}
The .* after custom means 0 or more characters after the word "custom", and the question mark limits the regex to as few character as possible, meaning that it will break on the next } that it can find.
If you want an alternative solution without regex:
String a = "{Action}{RequestId}{Custom_21_addtion}{custom_22_substration}{Imapact}{assest}{custom_23_multiplication}";
String[] b = a.split("}");
StringBuilder result = new StringBuilder();
for(String c : b) {
// if you want case sensitivity, drop the toLowerCase()
if(c.toLowerCase().contains("custom"))
result.append(c).append("}");
}
System.out.println(result.toString());
you can do it sth like this:
StringTokenizer st = new StringTokenizer(yourString, "{");
List<String> llista = new ArrayList<String>():
Pattern pattern = Pattern.compile("(\W|^)custom(\W|$)", Pattern.CASE_INSENSITIVE);
while(st.hasMoreTokens()) {
String string = st.nextElement();
Matcher matcher = pattern.matcher(string);
if(matcher.find()){
llista.add(string);
}
}
Another solution:
String inputString = "{Action}{RequestId}{Custom}{Custom_21_addtion}{custom_22_substration}{Imapact}{assest}" ;
String strTokens[] = inputString.split("\\}");
for(String str: strTokens){
Pattern pattern = Pattern.compile( "custom", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(inputString);
if (matcher.find()) {
System.out.println("Tag Name:" + str.replace("{",""));
}
}

Matcher. How to get index of found group?

I have sentence and I want to calculate words, semiPunctuation and endPunctuation in it.
Command "m.group()" will show String result. But how to know which group is found?
I can use method with "group null", but it is sounds not good.
String input = "Some text! Some example text."
int wordCount=0;
int semiPunctuation=0;
int endPunctuation=0;
Pattern pattern = Pattern.compile( "([\\w]+) | ([,;:\\-\"\']) | ([!\\?\\.]+)" );
Matcher m = pattern.matcher(input);
while (m.find()) {
// need more correct method
if(m.group(1)!=null) wordCount++;
if(m.group(2)!=null) semiPunctuation++;
if(m.group(3)!=null) endPunctuation++;
}
You could use named groups to capture the expressions
Pattern pattern = Pattern.compile( "(?<words>\\w+)|(?<semi>[,;:\\-\"'])|(?<end>[!?.])" );
Matcher m = pattern.matcher(input);
while (m.find()) {
if (m.group("words") != null) {
wordCount++;
}
...
}

how to deal with a string with regex

for example:
I have a string like this:
http://shop.vipshop.com/detail-97996-12358781.html
I want to use regex to find 97996 and 12358781
java code is appreciated
Many thanks.
String str="http://shop.vipshop.com/detail-97996-12358781.html";
String regex ="\-d{5}\-";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
System.out.println(matcher.group());
but it was wrong
Try this
String str="http://shop.vipshop.com/detail-97996-12358781.html";
String regex =".*detail-(\\d+)-(\\d+).html";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
if(matcher.matches()){
System.out.println(matcher.group(1) + "|" + matcher.group(2));
}
You have to invoke either Matcher#find() or Matcher#matches() to actually get the matches. In this case, you would need the former one, as you are only finding a part of string matching the regex.
And you can use + quantifier to get any length of digit. Try using this:
String regex ="\\d+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
Two lines:
String num1 = str.replaceAll(".*-(\\d+)-.*", "$1");
String num2 = str.replaceAll(".*-(\\d+)\\..*", "$1");
String str = "http://shop.vipshop.com/detail-97996-12358781.html";
String regex = "(?<=detail-)(\\d+)-(\\d+)(?=\\.html)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
matcher.find();
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
output:
97996 12358781
You have used a quantifier (5) and still the 1235... is 8 characters long?
Is it always 5 and 8 can you use:
"([\\d]{5,8})"
The matches captured into backreferences
But if you need to find in the specific form detail-NUMBER-NUMBER.html you can use:
"detail-([\\d]*)-([\\d]*).html"
The matches captured in [1] and [2]
you can use this:
String str="http://shop.vipshop.com/detail-97996-12358781.html";
String regex ="[^0-9]+([0-9]+)[^0-9]+([0-9]+).+";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
if(matcher.matches()){
System.out.println(matcher.group(1) + " " + matcher.group(2));
}
for advance tutorial go to this link
RegEx tutorial
and Regular Expression tutorial
You should add
if(matcher.find()){
}
on
System.out.println(matcher.group());
then your code is:
String str="http://shop.vipshop.com/detail-97996-12358781.html";
String regex ="\\d{5,}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
if(matcher.find()){
System.out.println(matcher.group());
}

regex extract string between two characters

I would like to extract the strings between the following characters in the given string using regex in Java:
/*
1) Between \" and \" ===> 12222222222
2) Between :+ and # ===> 12222222222
3) Between # and > ===> 192.168.140.1
*/
String remoteUriStr = "\"+12222222222\" <sip:+12222222222#192.168.140.1>";
String regex1 = "\"(.+?)\"";
String regex2 = ":+(.+?)#";
String regex3 = "#(.+?)>";
Pattern p = Pattern.compile(regex1);
Matcher matcher = p.matcher(remoteUri);
if (matcher.matches()) {
title = matcher.group(1);
}
I am using the above given code snippet, its not able to extract the strings that I want it to. Am I doing anything wrong? Meanwhile, I am quite new to regex.
The matches() method attempts to match the regular expression against the entire string. If you want to match a part of the string, you want the find() method:
if (matcher.find())
You could, however, build a single regular expression to match all three parts at once:
String regex = "\"(.+?)\" \\<sip:\\+(.+?)#(.+?)\\>";
Pattern p = Pattern.compile(regex);
Matcher matcher = p.matcher(remoteUriStr);
if (matcher.matches()) {
title = matcher.group(1);
part2 = matcher.group(2);
ip = matcher.group(3);
}
Demo: http://ideone.com/8t2EC
If your input always looks like that and you always want the same parts from it you can put that in a single regex (with multiple capturing groups):
"([^"]+)" <sip:([^#]+)#([^>]+)>
So you can then use
Pattern p = Pattern.compile("\"([^\"]+)\" <sip:([^#]+)#([^>]+)>");
Matcher m = p.matcher(remoteUri);
if (m.find()) {
String s1 = m.group(1);
String s2 = m.group(2);
String s3 = m.group(3);
}

Categories