Concatenation of a string and a regex in java - java

I want to concatenate a regex and a string and want to compare the resultant string with another string.How can I do that in java?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatching {
public static void main(String aaa[])
{
String testStr="anjaneyPANDEY";
String regEx = "([A-Z])";
Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(testStr);
String st="anjaney"+regEx;
if(testStr.matches(st))
System.out.println("YES");
else System.out.println("NO");
}
}

It seems that you forgot to add + in your regex to let it match one or more uppercase character. You should use [A-Z]+ since matches checks if entire string can be matched with regex.
Also you should use created Matcher instance instead of testStr.matches(st) to not recompile every time your pattern. So your code can look like
String regEx = "([A-Z]+)";
String st = "anjaney" + regEx;
Pattern pattern = Pattern.compile(st);
Matcher matcher = pattern.matcher(testStr);
if (matcher.matches())
System.out.println("YES");
else
System.out.println("NO");
This approach is OK if you know that string you want to combine with regex doesn't have any regex metacharacters like ( * + and so on.
But if you are not sure about it then you need to create escape such metacharacters. In that case you can use Pattern.quote() method. So instead of
String st = "anjaney" + regEx;
you can use
String st = Pattern.quote("anjaney") + regEx;

try it:
String testStr = "anjaneyPANDEY";
String regEx = "([A-Z])+";
String st = "anjaney" + regEx;
if (testStr.matches(st)) {
System.out.println("YES");
} else {
System.out.println("NO");
}

Related

How to trim a string using regex?

I have this alphabet: {'faa','fa','af'}
and I have this string: "faaf"
I have this regex: "(faa|fa|af)*" which helps me match the string with the alphabet.
How do I make Java trim my string into: {fa,af}, which is the correct way to write the string: "faaf" based on my alphabet?
here is my code:
String regex = "(faa|fa|af)*";
String str = "faaf";
boolean isMatch = Pattern.matches(regex, str);
if(isMatch)
{
//trim the string
while(str.length()!=0)
{
Pattern pattern = Pattern.compile("^(faa|fa|af)(faa|fa|af)*$");
Matcher mc = pattern.matcher(str);
if (mc.find())
{
String l =mc.group(1);
alphabet.add(l);
str = str.substring(l.length());
System.out.println("\n"+ l);
}
}
}
Thanks to Aaron who helped me with this problem.
You need a loop.
Pattern pattern = Pattern.compile(regex + "*");
LinkedList<String> parts = new LinkedList<>();
while (!str.isEmpty()) {
Matcher m = pattern.matcher(str);
if (!m.matches()) { // In the first loop step.
break;
}
parts.addFirst(m.group(1)); // The last repetition matching group.
str = str.substring(0, m.start(1));
}
String result = parts.stream().collect(Collectors.joining(", ", "{", "}"));
This utilizes that a match (X)+ will yield in m.group(1) the last occurrence's value of X.
Unfortunately the regex module does not provide a bored-open matches, such as the overloaded replaceAll with a lambda working on a single MatchResult.
Note that matches applies to the entire string.

How can I grep same format substrings within a long string by java regular expression?

For example, I want to grep both /css/screen/shared/styles.css and /css/screen/nol/styles.css from this long string:
#import "/css/screen/shared/styles.css";
#import "/css/screen/nol/styles.css";
Note that this long string contains 2 lines, it should look like this in java code:
String sentence = "#import \"/css/screen/nol/styles.css\";\n#import \"/css/screen/shared/styles.css\";";
So far, I have:
"#import\\s\"(.*?)\";\n"
it only identifies the "/css/screen/shared/styles.css", but ignores the "/css/screen/nol/styles.css".
Here is my code:
public static String getImportCSS(String sentence){
String result = "";
if(sentence.length() == 0) return null;
if(sentence.indexOf("#import ") != -1){
Pattern regex = Pattern.compile("#import\\s\"(.*)\";");
Matcher regexMatcher = regex.matcher(sentence);
if(regexMatcher.find()){
for(int i = 0; i <= regexMatcher.groupCount(); i++){
result = regexMatcher.group(1);
}
}
return result;
}
return null;
}
What am I doing wrong here? Thanks!
You cannot match the second string because your regex has an LF (\n) at the end.
Remove it, and the pattern will find both the strings. However, I'd advise to use a negated character class [^"]* (zero or more characters other than a ") rather than a lazy dot matching since the strings should not contain a double quote:
#import\s*\"([^"]*)\";
See the regex demo
Java demo:
String str = "#import \"/css/screen/shared/styles.css\";\n#import \"/css/screen/nol/styles.css\";";
Pattern ptrn = Pattern.compile("#import\\s*\"([^\"]*)\";");
Matcher matcher = ptrn.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1));
}

Java regular expression to validate and extract some values

I want to extract all three parts of the following string in Java
MS-1990-10
The first part should always be 2 letters (A-Z)
The second part should always be a year
The third part should always be a number
Does anyone know how can I do that using Java's regular expressions?
You can do this using java's pattern matcher and group syntax:
Pattern datePatt = Pattern.compile("([A-Z]{2})-(\\d{4})-(\\d{2})");
Matcher m = datePatt.matcher("MS-1990-10");
if (m.matches()) {
String g1 = m.group(1);
String g2 = m.group(2);
String g3 = m.group(3);
}
Use Matcher's group so you can get the patterns that actually matched.
In Matcher, the matches inside parenthesis will be captured and can be retrieved via the group() method. To use parenthesis without capturing the matches, use the non-capturing parenthesis (?:xxx).
See also Pattern.
public static void main(String[] args) throws Exception {
String[] lines = { "MS-1990-10", "AA-999-12332", "ZZ-001-000" };
for (String str : lines) {
System.out.println(Arrays.toString(parse(str)));
}
}
private static String[] parse(String str) {
String regex = "";
regex = regex + "([A-Z]{2})";
regex = regex + "[-]";
// regex = regex + "([^0][0-9]+)"; // any year, no leading zero
regex = regex + "([12]{1}[0-9]{3})"; // 1000 - 2999
regex = regex + "[-]";
regex = regex + "([0-9]+)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
if (!matcher.matches()) {
return null;
}
String[] tokens = new String[3];
tokens[0] = matcher.group(1);
tokens[1] = matcher.group(2);
tokens[2] = matcher.group(3);
return tokens;
}
This is a way to get all 3 parts with a regex:
public class Test {
public static void main(String... args) {
Pattern p = Pattern.compile("([A-Z]{2})-(\\d{4})-(\\d{2})");
Matcher m = p.matcher("MS-1990-10");
m.matches();
for (int i = 1; i <= m.groupCount(); i++)
System.out.println(m.group(i));
}
}
String rule = "^[A-Z]{2}-[1-9][0-9]{3}-[0-9]{2}";
Pattern pattern = Pattern.compile(rule);
Matcher matcher = pattern.matcher(s);
regular matches year between 1000 ~ 9999, u can update as u really need.

Extract every complete word that contains a certain substring

I'm trying to write a function that extracts each word from a sentence that contains a certain substring e.g. Looking for 'Po' in 'Porky Pork Chop' will return Porky Pork.
I've tested my regex on regexpal but the Java code doesn't seem to work. What am I doing wrong?
private static String foo()
{
String searchTerm = "Pizza";
String text = "Cheese Pizza";
String sPattern = "(?i)\b("+searchTerm+"(.+?)?)\b";
Pattern pattern = Pattern.compile ( sPattern );
Matcher matcher = pattern.matcher ( text );
if(matcher.find ())
{
String result = "-";
for(int i=0;i < matcher.groupCount ();i++)
{
result+= matcher.group ( i ) + " ";
}
return result.trim ();
}else
{
System.out.println("No Luck");
}
}
In Java to pass \b word boundaries to regex engine you need to write it as \\b. \b represents backspace in String object.
Judging by your example you want to return all words that contains your substring. To do this don't use for(int i=0;i < matcher.groupCount ();i++) but while(matcher.find()) since group count will iterate over all groups in single match, not over all matches.
In case your string can contain some special characters you probably should use Pattern.quote(searchTerm)
In your code you are trying to find "Pizza" in "Cheese Pizza" so I assume that you also want to find strings that same as searched substring. Although your regex will work fine for it, you can change your last part (.+?)?) to \\w* and also add \\w* at start if substring should also be matched in the middle of word (not only at start).
So your code can look like
private static String foo() {
String searchTerm = "Pizza";
String text = "Cheese Pizza, Other Pizzas";
String sPattern = "(?i)\\b\\w*" + Pattern.quote(searchTerm) + "\\w*\\b";
StringBuilder result = new StringBuilder("-").append(searchTerm).append(": ");
Pattern pattern = Pattern.compile(sPattern);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
result.append(matcher.group()).append(' ');
}
return result.toString().trim();
}
While the regex approach is certainly a valid method, I find it easier to think through when you split the words up by whitespace. This can be done with String's split method.
public List<String> doIt(final String inputString, final String term) {
final List<String> output = new ArrayList<String>();
final String[] parts = input.split("\\s+");
for(final String part : parts) {
if(part.indexOf(term) > 0) {
output.add(part);
}
}
return output;
}
Of course it is worth nothing that doing this will effectively be doing two passes through your input String. The first pass to find the characters that are whitespace to split on, and the second pass looking through each split word for your substring.
If one pass is necessary though, the regex path is better.
I find nicholas.hauschild's answer to be the best.
However if you really wanted to use regex, you could do it as such:
String searchTerm = "Pizza";
String text = "Cheese Pizza";
Pattern pattern = Pattern.compile("\\b" + Pattern.quote(searchTerm)
+ "\\b", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(text);
while (matcher.find()) {
System.out.println(matcher.group());
}
Output:
Pizza
The pattern should have been
String sPattern = "(?i)\\b("+searchTerm+"(?:.+?)?)\\b";
You want to capture the whole (pizza)string.?: ensures you don't capture a part of the string twice.
Try this pattern:
String searchTerm = "Po";
String text = "Porky Pork Chop oPod zzz llPo";
Pattern p = Pattern.compile("\\p{Alpha}+" + substring + "|\\p{Alpha}+" + substring + "\\p{Alpha}+|" + substring + "\\p{Alpha}+");
Matcher m = p.matcher(myString);
while(m.find()) {
System.out.println(">> " + m.group());
}
Ok, I give you a pattern in raw style (not java style, you must double escape yourself):
(?i)\b[a-z]*po[a-z]*\b
And that's all.

Java Word Regular Expression

I am quite bad at Java Regular expression so I hope you guys will help me.
String variable = "My life is better ";
String variable2 = "My life01 is better";
Now I have to write a code which would return true if the string has only "life"
So I should get TRUE only for variable not for variable2 because it has life but "01" too.
~thanks.
I have tried
if (variable.contains("life")){
System.out.println("TRUE");}
It return TRUE for both.
See solution :
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
/**
* #param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Pattern p = Pattern.compile("\\blife\\b");
Matcher m = p.matcher("life0 is better");
boolean b = m.find();
System.out.println(b);
}
}
Use word boundary \b matches.
See the Java Pattern documentation for details.
Note that you may need to write it at \\b to get proper escaping. The pattern needs the string \b, which when used in .java code (and not read e.g. from a file!) needs to be written in Java-escaped form as "\\blife\\b".
Use the following regex: -
"\blife\b"
with Pattern and Matcher class. This will match for complete word. (\b denote word boundary)
You would have to use Matcher#find method, to check whether a string contains this pattern.
Note: - If you want to use String.matches, which would be appropriate here, than going with Pattern and Matcher, you would have to add .* in the front and the end. Because, String.matches matches the whole string.
For e.g: -
String str = "asdf life asdf";
System.out.println(str.matches("\\blife\\b")); // Prints false
System.out.println(str.matches(".*\\blife\\b.*")); // Prints true
In the second Regex, .* matches the string before and after life.
Use the String.matches() method and Regex \b to match word boundaries.
public class StringChecker {
String variable1 = "My life is better ";
String variable2 = "My life01 is better";
System.out.println("variable1: " + containsString(variable1));
System.out.println("variable1: " + containsString(variable2));
//Returns true if the string contains "life"
public boolean containsString(String s){
return s.matches(".*(\\blife\\b).*");
}
}
package com.rampukar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class DemoRegx {
public static void main(String[] args) {
String email = "ram#example.com";
String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*#" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
Pattern pt_mail = Pattern.compile(EMAIL_PATTERN);
Matcher mt_mail = pt_mail.matcher(email);
if (mt_mail.matches()) {
System.out.println("Valid");
} else {
System.out.println("In-Valid");
}
System.out.println("---------------------");
String name = "Ram Pukar";
Pattern pt_name = Pattern.compile("[a-zA-Z ]{2,}");
Matcher mt_name = pt_name.matcher(name);
if (mt_name.matches()) {
System.out.println("Valid");
} else {
System.out.println("In-Valid");
}
System.out.println("---------------------");
String user = "ram123";
Pattern pt_user = Pattern.compile("[a-zA-Z0-9]{2,}");
Matcher mt_user = pt_user.matcher(user);
if (mt_user.matches()) {
System.out.println("Valid");
} else {
System.out.println("In-Valid");
}
}
}

Categories