Java expression for Embedded parameter - java

I am defining java expression for my pattern but it does not work.
Here is the text that I want to define a pattern for:
"sometext {10} some text {25} sometext".
Named parameters are {10}, {25}, ....
I used pattern like this: "({\d+})*" but It doesn't work and I received exception:
Caused by: java.util.regex.PatternSyntaxException: Illegal repetition near index 0
({\d+})*
Here is the code I have:
public static final Pattern pattern = Pattern.compile("({\\d+})*");
public static void main(String[] args) {
String s = "{10}ABC{2}";
Matcher matcher = pattern .matcher(s);
while (matcher.find()) {
System.out.println(matcher.group(1));
}
}
Can someone explain what I am wrong here? Thanks.

There are a few issues with your Pattern.
Firstly you don't need the outer parenthesis, as you're not defining a group save from the main group.
Secondly you must escape the curly brackets (\\{), otherwise they will be interpreted as a quantifier.
Thirdly you don't need the last quantifier (*), as you're iterating over matches within the same input String
So your Pattern will look something like "\\{\\d+\\}".
More info on Java Patterns here.
Edit -- example
String input = "sometext {10} some text {25} sometext";
Pattern p = Pattern.compile("\\{\\d+\\}");
Matcher m = p.matcher(input);
while (m.find()) {
System.out.println(m.group());
}
Output:
{10}
{25}

{ is a special character in regex, just double-escape it \\{. Same for }.
Also take into account that if you use *, it will also match empty strings.

Related

Regex look ahead to seperate string into tokens

I currently have the following code which allows me to find matches from a String.
I need to be able to find all words similar to 64xand split them up into tokens, so I'll get 64 and x as the output.
I have looked at regexs lookahead and this does not solve the issue, is there a way to do this without creating a new arraylist to store matches similar to 64x then splitting them up?
String input = "Hello world 65x";
ArrayList<String> userInput = new ArrayList<>();
Matcher isMatch = Pattern.compile("[0-9]*+[a-zA-Z]")
.matcher(input);
while (isMatch.find()) {
userInput.add(isMatch.group());
}
You can try the following regular expression:
\b(\p{Digit}+)(\p{Alpha})\b
Additionally, if you plan to use the regular expression very often, it is recommended to use a constant in order to avoid recompile it each time, e.g.:
private static final Pattern REGEX_PATTERN =
Pattern.compile("\\b(\\p{Digit}+)(\\p{Alpha})\\b");
public static void main(String[] args) {
String input = "Hello world 65x";
Matcher matcher = REGEX_PATTERN.matcher(input);
while (matcher.find()) {
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
}
}
Output:
65
x
No need of lookaheads, you can use nested captured groups:
Matcher isMatch = Pattern.compile("\\b([0-9]+)([a-zA-Z])\\b");
Group #1 will contain 65 and group #2 will contain x.
Better to add \\b (word boundary) on either side to avoid matching abc56xyz
You just need to use Matcher.group(int). This lets you extract pieces of the matched text. Read about caputring groups here. A regex that contains capturing groups is \\b([0-9]+)([a-zA-Z])\\b (as given by anubhava).

How to crop multiple text from String in Java?

I want to crop a portion of String:
" this is Test [ABC:123456] Sting with multiple properties [ABC:98765] ..."
So in result i want to crop String between "[ ]". {Here ABC:12345 and ABC:98765}
Note There can be n number of property.
what is the Best way to get result.
public static void main(String[] args) {
String input = "test bla [ABC56465:asd] asdasdqwd [DEF:345]";
Pattern pattern = Pattern.compile("\\[(.*?)\\]");
Matcher match = pattern.matcher(input);
while(match.find()){
System.out.println(match.group());
}
}
Follow the Tutorials from Niels. This could be solution.
To get the output without the "[ ]" just replace:
System.out.println(match.group());
With:
System.out.println(match.group(1));
as mentioned in the comments.
You need to define you pattern and do a regex matching, extrating the group you need.
See http://tutorials.jenkov.com/java-regex/matcher.html
for a tutorial on regex. (Especially the find() start() and end() section)
In your case the pattern should be very simple.
In Java, you can do this:
String resultString = subjectString.replaceAll("\\[[^\\]]*\\]", "");
Explanation
\[ matches the opening bracket
The negated character class [^\]]* matches any character that is not a closing bracket
\] matches the closing bracket
We replace with the empty string

Regular expression -- ignore part of a matched string

I have text that I want to fined some thing like this
"Name DAVID"
I want to match "DAVID" in this, larger, text.
I tried use a regular expression like this:
(Name(.*))
and also
(?:Name(.*))
but this also matched "Name," and I only want to match "David".
Just drop the extra parens:
"Name (.*)"
Even that is probably excessive, you probably want something more like:
"Name (\w*)"
to catch exactly the characters that you want.
You need to use a Matcher. This snippet of code worked for me
public static void main(String[] asdf) {
String text = "NAME David";
Pattern p = Pattern.compile("NAME (.+)");
Matcher m = p.matcher(text);
if (m.matches()){
System.out.println(m.group(1));
}
}
Note that m.matches() is mandatory or m.group(1) will throw java.lang.IllegalStateException: No match found
String's matches()
public static void main(String[] args) {
String regex = "^Name(.+)$";
System.out.println("Name".matches(regex));
System.out.println("Name MIKE".matches(regex));
System.out.println("Name DAVID".matches(regex));
}
For some reason, it doesn't look like your question had yet been answered with a correct regex for what you requested.
Here is what you are looking for:
(?<=Name )DAVID
This only matches DAVID, in the proper context (see demo).
You probably know this, but this is a way to test a string with this regex:
Pattern regex = Pattern.compile("(?<=Name )DAVID");
Matcher regexMatcher = regex.matcher(subjectString);
foundMatch = regexMatcher.find();
Explain Regex
(?<= # look behind to see if there is:
Name # 'Name '
) # end of look-behind
DAVID # 'DAVID'

java regular expression word without ending with dot

I need to print the simple bind variable names in the SQL query.
I need to print the words starting with : character But NOT ending with dot . character.
in this sample I need to print pOrg, pBusinessId but NOT the parameter.
The regular expression ="(:)(\\w+)^\\." is not working.
Could you help in correcting the regular expression.
Thanks
Peddi
public void testMethod(){
String regEx="(:)(\\w+)([^\\.])";
String input= "(origin_table like 'I%' or (origin_table like 'S%' and process_status =5))and header_id = NVL( :parameter.number1:NULL, header_id) and (orginization = :pOrg) and (businsess_unit = :pBusinessId";
Pattern pattern;
Matcher matcher;
pattern = Pattern.compile(regEx);
matcher = pattern.matcher(input);
String grp = null;
while(matcher.find()){
grp = matcher.group(2);
System.out.println(grp);
}
}
You can try with something like
String regEx = "(:)(\\w+)\\b(?![.])";
(:)(\\w+)\\b will make sure that you are matching only entire words starting with :
(?![.]) is look behind mechanism which makes sure that after found word there is no .
This regex will also allow :NULL so if there is some reason why it shouldn't be matched share it with us.
Anyway to exclude NULL from results you can use
String regEx = "(:)(\\w+)\\b(?![.])(?<!:NULL)";
To make regex case insensitive so NULL could also match null compile this pattern with Pattern.CASE_INSENSITIVE flag like
Pattern pattern = Pattern.compile(regEx,Pattern.CASE_INSENSITIVE);
Since it looks like you're using camelcase, you can actually simplify things a bit when it comes to excluding :NULL:
:([a-z][\\w]+)\\b(?!\\.)
And $1 will return your variable names.
Alternative that doesn't rely on negative lookahead:
:([a-z][\\w]+)\\b(?:[^\\.]|$)
You can try:
Pattern regex = Pattern.compile("^:.*?[^.]$");
Demo

pattern matcher

I have the following pattern matcher.
Pattern pat = Pattern.compile("[^a-z][^,.:;]");
How do I include the ] character itself in it ?
You need to escape it.
\\]
See this.
] is a special character used to denote the end of a character class so it needs to be escaped:
Pattern pat = Pattern.compile("[^a-z][^,.:;\\]]");
You can do it by using escape character \ like this \\].
You don't really need to escape it. There is a special rule in Regular Expressions, where if you want the actual ] character in your list, it has to be the first element on it. And it'll work just fine. Give this code a try:
public static void main(String[] args){
String texto = "[]hello[]";
Pattern p = Pattern.compile("[]]+");
Matcher m = p.matcher(texto);
while(m.find()){
System.out.println(m.group());
}
}
The standard way is the use of quote function of the Pattern class. This function return the literal pattern String for the specified String.
String myLiteralString = Pattern.quote(",.[:;");
Pattern pat = Pattern.compile("[^a-z][^" + myLiteralString + "]");
This method escape all special characters of the regular expression syntax.

Categories