This question already has answers here:
How to escape text for regular expression in Java?
(8 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 5 years ago.
This is what I am trying to do...
Enter Pattern
//*//_//*
Enter Text
hello *_* how are you?
No match found
I know to avoid treating above characters as Metacharacters I need to use these escape sequences.
I don't know how to do with _ (underscore sigh)
I even tried
//* _ //*
Still it didn't work.
code
Scanner sc = new Scanner(System.in);
System.out.println("Enter pattern");
Pattern pattern = Pattern.compile(sc.nextLine());
System.out.println("Enter Text");
Matcher matcher = pattern.matcher(sc.nextLine());
boolean found = false;
while(matcher.find())
{
System.out.println("I found the text" + matcher.group() + "Starting index" + matcher.start() + "Ending at index" + matcher.end());
found = true;
}
if(!found)
{
System.out.println("No match found");
}
If you are trying to match the *_* part of the input, try the following pattern:
\*\_\*
Related
This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Closed 2 years ago.
In this string:
jdbc:sqlserver://testserver.apple.com\A4534:54623
I want to match "testserver" "apple.com//A4534" and "54623"
Regex:
jdbc:sqlserver:(.+)\.(.+):(.+)
But I get,
"testserver.apple" "com//A4534" and "54623"
I suggest the following regex:
jdbc:sqlserver:\/\/(\w+).(.+):(\w+)
Explanation:
\/ specifies the /
\w+ specifies word character i.e. [A-Za-z0-9_]+
Check this for a demo.
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String str = " jdbc:sqlserver://testserver.apple.com\\A4534:54623";
Pattern pattern = Pattern.compile("jdbc:sqlserver:\\/\\/(\\w+).(.+):(\\w+)");
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group(1) + "\n" + matcher.group(2) + "\n" + matcher.group(3));
}
}
}
Output:
testserver
apple.com\A4534
54623
This question already has answers here:
Check and extract a number from a String in Java
(16 answers)
Closed 3 years ago.
How to prevent or catch String for which the user has entered an int or a number?
ex.
String name=JOptionPane.showInputDialog("Enter Dog owner's First and Last name");
then if I input Dave Har1234, I want to catch that number on the input then return to the JOptionPane.
I think you should only check if the complete string is a number. For that use the following check inp.matches("\\d")
You can check a complete example here https://onecompiler.com/java/3v5pffkbz
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main
{
public static void main(String[] args) {
String inputStr = "abc00123xyz4560"; // Input String for matching
String regexStr = "[0-9]+"; // Regex to be matched
Pattern pattern = Pattern.compile(regexStr);
Matcher matcher = pattern.matcher(inputStr);
while (matcher.find()) {
System.out.println("find() found substring \"" + matcher.group()
+ "\" starting at index " + matcher.start()
+ " and ending at index " + matcher.end());
}
}
}
you can try this code to catch if user has entered any number in between string.
Can run and check code here: https://www.onlinegdb.com/online_java_compiler
This question already has answers here:
Java: splitting a comma-separated string but ignoring commas in parentheses
(4 answers)
Closed 5 years ago.
I want to cut this text
UNIT=1111,SPACE=(TRK,0),DISP=(MOD,DELETE,DELETE),DSN=UUU.AAAAA.BBBBB
Result :
UNIT=1111
SPACE=(TRK,0)
DISP=(MOD,DELETE,DELETE)
DSN=UUU.AAAAA.BBBBB
I tried myself but I m so noob with regular expression, I used (\S+)=(\S+) to cut it but it not work correct.
Someone could help me ?
Here is my java code
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "(\\S+)=(\\S+)";
final String string = "UNIT=1111,SPACE=(TRK,0),DISP=(MOD,DELETE,DELETE),DSN=UUU.AAAAA.BBBBB"
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
You can use this negative lookahead regex for splitting:
String[] arr = str.split(",(?![^()]*\\))");
This is assuming ( and ) are all balanced and unescaped.
RegEx Demo
RegEx Breakup:
,: Match a literal comma
(?![^()]*\\)): Negative lookahead to assert that comma is not inside a (...)
Working code ise here https://regex101.com/r/G355nS/2
^(UNIT=\d{4}),(SPACE=\S+\d+\)),(DISP=\S+\)),(DSN=[\S+.]*)$
This question already has an answer here:
regex: How to escape backslashes and special characters?
(1 answer)
Closed 8 years ago.
I was trying to escape a Json string as an input via Scanner and print to console,
i was not able to escape " \ " by replacing it with " \\ ",
I'm getting PatternSyntaxException
Here is my code
Scanner s = new Scanner(System.in);
String str = s.next();
String s3 = "";
if (str.contains("\\")) {
s3 = str.replaceAll("\\", "\\\\");
System.out.println(s3);
}
Here is my input to scanner
{"name":"nokia"}\
Help me please !
If you are using regex you have to use 4 backslashes \\\\ to parse the backslash as a literal.
So use s3 = str.replaceAll("\\\\", someOtherString);
This question already has answers here:
Match multiline text using regular expression
(4 answers)
Closed 9 years ago.
I need a regular expression to match the very first word within the following source:
WanRoutingProtocol=
Static
192.160.22.0/27
false
2004:BA2:78::50
=IAS
I just want to extract the very first word (In this case "Static") using a regular expression in java.
The blank lines contains multiple newlines.
I'm using the following regex
"^(\\n)+Static.*IAS"
but this is not working.
Use the following regex. Expression assumes that the input would always start and end with the keywords "WanRoutingProtocol" and "IAS" and would fetch any keyword present at the place of "Static".
^WanRoutingProtocol=\\s*(.*)[\\s\\w\\./:]*=IAS$
Here's how you could do this in Java. (There's no need to use Pattern.MULTILINE)
String input = "WanRoutingProtocol=\n" +
" Static\n" +
"\n" +
"\n" +
"\n" +
" 192.160.22.0/27\n" +
" false\n" +
"\n" +
" 2004:BA2:78::50\n" +
"\n" +
"\n" +
" =IAS";
Pattern p = Pattern.compile("^WanRoutingProtocol=\\s*(.*)[\\s\\w\\./:]*=IAS$");
Matcher m = p.matcher(input);
while (m.find()) {
System.out.println(m.group(1)); // prints "Static"
}