regular expression to extract string from java code [duplicate] - java

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.

Related

Extracting a word from a message in java [duplicate]

This question already has answers here:
Java Find word in a String
(5 answers)
Find word in random string
(3 answers)
How to find index of whole word in string in java
(1 answer)
Regex to find a specific word in a string in java
(3 answers)
Closed 5 years ago.
I have a message that is of the format:
FixedWord1 variable1 FixedWord2 on FixedWord3 variable2/variable3, variable4 = variable5
I need to extract only variable3 from the above message.
Here is what I tried:
String example = "FixedWord1 variable1 FixedWord2 on FixedWord3 variable2/variable3, variable4 = variable5";
I know that the length of FixedWord3 is 6. So,
example.substring(example.lastIndexOf("FixedWord3") + 6 , example.lastIndexOf(",")); //To get {variable2}/{variable3}
And then,
String requiredString[] = example.split("/", 2); //requiredString[1] would contain {variable3} even if it contains /
Can you suggest a more efficient solution to this problem?
EDIT:
This regex should do the trick.
Pattern pattern = Pattern.compile(".+(Device).+[/]([A-Z].+)[,][ ].+");
Matcher matcher = pattern.matcher(yourstring);
if(matcher.matches())
System.out.println(matcher.group(2));
Assumption to make this work:
Variable2 has no slash '/' followed by upper case letter
Variable3 has no comma ',' followed by space ' '
Since you know that variable2 cannot contain a "/" and you know the length of FixedWord3 then how about this?
String deviceName = example.substring(example.lastIndexOf("Device") + 6, example.lastIndexOf(","));
String lastPart = deviceName.substring(deviceName.indexOf("/") + 1);
System.out.println(deviceName);
System.out.println(lastPart);
Prints:
SJ-ME3600X-185/GigabitEthernet0/4
GigabitEthernet0/4
Regex for the help.
One possible approach is catching the match that's after "{variable2}":
{variable2}\/{([^}]+)}
Then you can use Matcher and Pattern and maybe other tools to make it work in Java.
See here for explanation and live demo.
Using Regex patterns are the efficient way to extract the word from a message in java.
String s = "FixedWord1 {variable1} FixedWord2 on FixedWord3 {variable2}/{variable3}, {variable4} = {variable5}";
Pattern p = Pattern.compile("/(\\{([^}]*)\\})");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group(1));
}
Output {variable3}

Java Regular expression matching square brackets [duplicate]

This question already has answers here:
Regular expression to extract text between square brackets
(15 answers)
Closed 12 months ago.
I'm trying to do the following using regular expression (java replaceAll):
**Input:**
Test[Test1][Test2]Test3
**Output**
TestTest3
In short, i need to remove everything inside square brackets including square brackets.
I'm trying this, but it doesn't work:
\\[(.*?)\\]
Would you be able to help?
Thanks,Sash
You can try this regex:
\[[^\[]*\]
and replace by empty
Demo
Sample Java Source:
final String regex = "\\[[^\\[]*\\]";
final String string = "Test[Test1][Test2]Test3\n";
final String subst = "";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
final String result = matcher.replaceAll(subst);
System.out.println(result);
Your original pattern works for me:
String input = "Test[Test1][Test2]Test3";
input = input.replaceAll("\\[.*?\\]", "");
System.out.println(input);
Output:
TestTest3
Note that you don't need the parentheses inside the brackets. You would use that if you planned to capture the contents in between each pair of brackets, which in your case you don't need. It isn't wrong to have them in there, just not necessary.
Demo here:
Rextester
\[\w+]
This works for me, this regex matches all the words which are enclosed in square brackets.

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)

String pattern matching with regular expression in java [duplicate]

This question already has answers here:
Java regular expressions and dollar sign
(5 answers)
Closed 8 years ago.
I'm trying pattern matching expression for a below string. But it doesn't work. could you anybody help me on this ? Only Alphanumeric and underscore allowed inside,Both side
$ sign will be there. Ex strings: Test_1,23_test_2,test3.
String text = "$test_1$";
Pattern p = Pattern.compile("$([A-Za-z0-9_])$");
Matcher m = p.matcher(text);
m.matches();
if (m.find()) {
System.out.println("Matched: " + m.group(1));
} else {
System.out.println("No match.");
}
Your regex should be:
Pattern p = Pattern.compile("(\\$[A-Za-z0-9_]*\\$)");
You could simply do...
s.matches("\\$[a-zA-Z0-9_]*\\$")
$ is a regex meta character and should be escaped, try this
Pattern p = Pattern.compile("\\$([A-Za-z0-9_]+)\\$");

Java Regular Expression checking for symbols [duplicate]

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 _)

Categories

Resources