How to match regex in java - 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
}

Related

How to extract value using patter and matcher in Java

Hey i am using Pattern and matcher to extract bugId value from given URL.
1. url=/prrq/viewReview.do?bugId=bgid12
2. url=/prrq/viewReview.do?queueName=abc&bugId=bgid12
This is what i am doing -
String getbugIdPattern = ".*[?&]bugId=([^&]+).*";
Pattern bugIdp = Pattern.compile(getbugIdPattern);
Matcher bugidm = bugIdp.matcher(url);
if (bugidm.matches() ) {
String bugid = bugidm.group(1);
}
But i am not getting any match result.

Match everything after and before something regex Java

Here is my code:
String stringToSearch = "https://example.com/excludethis123456/moretext";
Pattern p = Pattern.compile("(?<=.com\\/excludethis).*\\/"); //search for this pattern
Matcher m = p.matcher(stringToSearch); //match pattern in StringToSearch
String store= "";
// print match and store match in String Store
if (m.find())
{
String theGroup = m.group(0);
System.out.format("'%s'\n", theGroup);
store = theGroup;
}
//repeat the process
Pattern p1 = Pattern.compile("(.*)[^\\/]");
Matcher m1 = p1.matcher(store);
if (m1.find())
{
String theGroup = m1.group(0);
System.out.format("'%s'\n", theGroup);
}
I want to to match everything that is after excludethis and before a / that comes after.
With "(?<=.com\\/excludethis).*\\/" regex I will match 123456/ and store that in String store. After that with "(.*)[^\\/]" I will exclude / and get 123456.
Can I do this in one line, i.e combine these two regex? I can't figure out how to combine them.
Just like you have used a positive look behind, you can use a positive look ahead and change your regex to this,
(?<=.com/excludethis).*(?=/)
Also, in Java you don't need to escape /
Your modified code,
String stringToSearch = "https://example.com/excludethis123456/moretext";
Pattern p = Pattern.compile("(?<=.com/excludethis).*(?=/)"); // search for this pattern
Matcher m = p.matcher(stringToSearch); // match pattern in StringToSearch
String store = "";
// print match and store match in String Store
if (m.find()) {
String theGroup = m.group(0);
System.out.format("'%s'\n", theGroup);
store = theGroup;
}
System.out.println("Store: " + store);
Prints,
'123456'
Store: 123456
Like you wanted to capture the value.
This may be useful for you :)
String stringToSearch = "https://example.com/excludethis123456/moretext";
Pattern pattern = Pattern.compile("excludethis([\\d\\D]+?)/");
Matcher matcher = pattern.matcher(stringToSearch);
if (matcher.find()) {
String result = matcher.group(1);
System.out.println(result);
}
If you don't want to use regex, you could just try with String::substring*
String stringToSearch = "https://example.com/excludethis123456/moretext";
String exclusion = "excludethis";
System.out.println(stringToSearch.substring(stringToSearch.indexOf(exclusion)).substring(exclusion.length(), stringToSearch.substring(stringToSearch.indexOf(exclusion)).indexOf("/")));
Output:
123456
* Definitely don't actually use this

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

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.

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++;
}
...
}

Java regex replace ignoring the first match

mediaSourceSpecificJunkCharacters=mediaSourceSpecificJunkCharacters+",";
Pattern p = Pattern.compile("\\[(.*?)\\],",Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher matcher = p.matcher(mediaSourceSpecificJunkCharacters);
while(matcher.find()) {
String stringToMatch=matcher.group(1);
System.out.println("string to match "+stringToMatch);
originalText=originalText.replaceAll(stringToMatch.trim(),"");
}
here originalText="this is data from youtube youtube1 youtube2 youtube3 youtube4";
and mediaSourceSpecificJunkCharacters=[youtube2],[youtube3],[youtube4]
the first match is youtube3 and not youtube2....so youtube2 never gets replaced...why is it so?
You don't even have youtube1 in your mediaSourceSpecificJunkCharacters. Change that to
String mediaSourceSpecificJunkCharacters = "[youtube1],[youtube2],[youtube3],[youtube4]";
and also change your pattern to
Pattern p = Pattern.compile("\\[(.*?)\\]", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
if you want to replace youtube4 too, the , at the end prevents this in your code.

Categories