Java Regular Expression checking for symbols [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Regular expression for excluding special characters
How would I check if a String contains a symbol? let's say I have this String
"SUGAR :::: SUGAR"
I would want to check if that string contains = the following Symbols
":,?,!##$%^&*()";
I tried this
Pattern p = Pattern.compile("[?,!,#,$,%,^,&,*,(,)]");
Matcher m = p.matcher("?");
boolean b = m.matches();
System.out.println(b);
But what if the text contains multiple occurrences of that symbol

Try this:
Pattern pat = Pattern.compile("[:?!##$%^&*()]");
String str = "SUGAR :::: SUGAR";
Matcher m = pat.matcher(str);
if (m.find()) {
System.out.println("string contains pattern");
}
The above will check if any part of the string contains at least one occurrence of any of the symbols in the pattern (no need to separate them with ,).

guava may be?
CharMatcher matcher = CharMatcher.anyOf(":,?,!##$%^&*()");
boolean result = matcher.matchesAnyOf("SUGAR :: SUGAR");
System.out.println(result);

Just simply match against a set containing these characters: [:,?!##$%^&*()]
If you want to check against the symbol -, position it to the end of the set: [...-]
There's no need to separate elements in a set by comma.
If you want to check for ANY non alphanumeric use \W instead: [\W_] (because \W does not match _)

Related

kotlin/java match a number in a string with a regular expression [duplicate]

This question already has answers here:
How to extract numbers from a string and get an array of ints?
(13 answers)
Closed 1 year ago.
For example, if I have these strings, is there any way I can get 123 of all these strings, or 777 or 888?
https://www.example.com/any/123/ and
https://www.example.com/any/777/123/ and
https://www.example.com/any/777/123/888
What I mean is how to match the first or second or the third last number in the string.
You can use capture groups to solve this as
val strList = listOf("https://www.example.com/any/777/123/888", "https://www.example.com/any/123/", "https://www.example.com/any/777/123/")
val intList = mutableListOf<Int>()
val regex = Regex("/?(\\d+)")
strList.forEach { str ->
regex.findAll(str).forEach {
intList.add(it.groupValues[1].toInt())
}
}
Assuming the digits all follow a slash and nothing intervenes,
(?<=/)\d+(?=/\d+){0}$ parses the last number
(?<=/)\d+(?=/\d+){1}$ parses the second to last number
(?<=/)\d+(?=/\d+){2}$ parses the third to last,
etc.
With Java, You can make use of the Pattern and Matcher class from the java.util.regex package.
e.g for your case above, you want to match integers - use \d Predefined character class to match digits.
String str = "https://www.example.com/any/777/123/";
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(str);
for(; matcher.find(); System.out.println(matcher.group()));
In the above you loop through the String finding matches, and printing each subsequent found match.

Repeating capture group in a regular expression

What would be the best way to parse the following string in Java using a single regex?
String:
someprefix foo=someval baz=anotherval baz=somethingelse
I need to extract someprefix, someval, anotherval and somethingelse. The string always contains a prefix value (someprefix in the example) and can have from 0 to 4 key-value pairs (foo=someval baz=anotherval baz=somethingelse in the example)
You can use this regex for capturing your intended text,
(?<==|^)\w+
Which captures a word that is preceded by either an = character or is at ^ start of string.
Sample java code for same,
Pattern p = Pattern.compile("(?<==|^)\\w+");
String s = "someprefix foo=someval baz=anotherval baz=somethingelse";
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
}
Prints,
someprefix
someval
anotherval
somethingelse
Live Demo

Need a Regex that extracts a string between two "delimiting" strings [duplicate]

This question already has answers here:
Java Regex Capturing Groups
(4 answers)
Closed 6 years ago.
I need to get the string between by_ and _on.
So far I have this, but don't understand how to truncate the actual "string delimiters":
by_(.*)_on
Sample input:
Files_by_wesasegeaazedude_on_January_26.jpg
Current Match:
by_wesasegeaazedude_on
Needed Match:
wesasegeaazedude
Your expression is good*. All you need to do is extracting the content of the first capturing group:
Pattern regex = Pattern.compile("by_(.*)_on");
String str = "Files_by_wesasegeaazedude_on_January_26.jpg";
Matcher m = regex.matcher(str);
if (m.find()) {
String res = m.group(1);
}
Demo.
* Well, almost good. If you expect inputs with multiple file names on the same line, you may want to consider using reluctant qualifier, i.e. by_(.*?)_on
I would do this without regular expressions.
int start = str.indexOf("by_");
int end = str.indexOf("_on", start + 1); // or lastIndexOf("_on"), for greedy match.
assert start > 0 && end > start;
String part = str.substring(start + 3, end);
You can simply use positive lookarounds:
String regex = "(?<=by_).*(?=_on)";
What this regex does is:
match anything: .*
that is preceded by by_: (?<=by_)
and followed by _on: (?=_on)

regular expression to extract string from java code [duplicate]

This question already has answers here:
Regex to replace all string literals in a Java file
(4 answers)
Closed 8 years ago.
The sample source code to match is
String string="welcome";
String k="a\"welcome";
I am using "(\"[^(\")]*\")" regex in java.
But this extracts
0:"welcome"
0:"a\"
Expected output is
0:"welcome"
0:"a\"welcome"
What change should i make in regex to get the expected output ?
Java source :
private static String pattern1="(\"[^(\")]*\")";
public void getStrings(){
Pattern r = Pattern.compile(pattern1);
Matcher m = r.matcher("String string=\"welcome\";\n" +
"String k=\"a\\\"welcome\";");
while(m.find()){
System.out.println("0:"+m.group(0));
}
}
Just use lookahead and lookbehind in your regex,,
(?<==)(".*?")(?=;)
Get the value from group index 1.
DEMO
Pattern r = Pattern.compile("(?<==)(\".*?\")(?=;)");
Matcher m = r.matcher("String string=\"welcome\";\n" +
"String k=\"a\\\"welcome\";");
while(m.find()){
System.out.println("0:"+m.group(1));
}
Output:
0:"welcome"
0:"a\"welcome"
OR
Use the greediness of *,
Pattern r = Pattern.compile("(\".*\")");
OR
It skips the double quotes which are preceded by a backslash,
Pattern r = Pattern.compile("(\\\".*?(?<=[^\\\\])\\\")");
Why do you even bother with variable assignment. You know that everything within "" is a string.
"(.+)"\s*; should do it just fine.

Java: extract the single matching groups from a string with regular expression [duplicate]

This question already has answers here:
How to split a string between letters and digits (or between digits and letters)?
(8 answers)
Closed 8 years ago.
I have this kind of string: 16B66C116B or 222A3*C10B
It's a number (with unknow digits) followed or by a letter ("A") or by a star and a letter ("*A"). This patter is repeated 3 times.
I want to split this string to have: [number,text,number,text,number,text]
[16, B, 66, C, 116, B]
or
[16, B, 66, *C, 116, B]
I wrote this:
String tmp = "16B66C116B";
String tmp2 = "16B66*C116B";
String pattern = "(\\d+)(\\D{1,2})(\\d+)(\\D{1,2})(\\d+)(\\D{1,2})";
boolean q = tmp.matches(pattern);
String a[] = tmp.split(pattern);
the pattern match right, but the splitting doesn't work.
(I'm open to improve my pattern string, I think that it could be write better).
You are misunderstanding the functionality of split. Split will split the string on the occurence of the given regular expression, since your expression matches the whole string it returns an empty array.
What you want is to extract the single matching groups (the stuff in the brackets) from the match. To achieve this you have to use the Pattern and Matcher classes.
Here a code snippet which will print out all matches:
Pattern regex = Pattern.compile("(\\d+)(\\D{1,2})(\\d+)(\\D{1,2})(\\d+)(\\D{1,2})");
Matcher matcher = regex.matcher("16B66C116B");
while (matcher.find()) {
for (int i = 1; i <= matcher.groupCount(); ++i) {
System.out.println(matcher.group(i));
}
}
Of course you can improve the regular expression (like another user suggested)
(\\d+)([A-Z]+)(\\d+)(\\*?[A-Z]+)(\\d+)([A-Z]+)
Try with this pattern (\\d)+|(\\D)+ and use Matcher#find() to find the next subsequence of the input sequence that matches the pattern.
Add all of them in a List or finally convert it into array.
String tmp = "16B66C116B";
String tmp2 = "16B66*C116B";
String pattern = "((\\d)+|(\\D)+)";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(tmp);
while (m.find()) {
System.out.println(m.group());
}

Categories

Resources