in my case i have a some word like
**********menu 1************
gaurav
saurav
amit
avanish
**********menu 2************
gauravqwe
sourav
anit
abhishek
now i want to check item "gaurav" from menu one or menu two.
if "gaurav" from menu 1 then return true else return false
i m try:
class Regex_Test {
public void checker(String Regex_Pattern){
Scanner Input = new Scanner(System.in);
String Test_String = Input.nextLine();
Pattern p = Pattern.compile(Regex_Pattern);
Matcher m = p.matcher(Test_String);
System.out.println(m.find());
}
}
public class CheckHere {
public static void main(String[] args) {
Regex_Test tester = new Regex_Test();
tester.checker("^[gsa][amv]"); // Use \\ instead of using \
}
}
but it returns true in case of "gauravqwe"
i need expression "string" for the above question
Condition string size is less then 15 character
Use the meta-character word boundaries \b
\bbgaurav\b
regex101 Demo
References:
http://www.regular-expressions.info/wordboundaries.html
Less than 15 characters
To do this in less than 15 characters you need a regex that looks like this:
^gaurav(\r|\Z)
This regex is a subset of following answer.
Description
I'd do it in one pass by building a Regex to grab the menu title of the matching entry. This regex will do the following:
find gaurav in the source string
return the menu name from the matching section
if the string does not contain a match then return will be empty
The Regex
[*]{9,}([^*]*?)[*]{9,}(?:(?![*]{9,}).)*^gaurav(?:\r|\Z)
Note this regex uses the following options: case-insensitive, multiline (with ^ and $ matching start and end of line), and dot matches new line (with . matching \n)
Explanation
This construct (?:(?![*]{9,}).)* is where all the magic happens. This forces the searching to move forward through the string, but does not allow the pattern matching to span multiple ********** delimited segments.
The ^ and (?:\n|\Z) constructs force the regex engine to match a full string, and not just the initial characters. Example: if you're looking for gaurav then gauravqwe will not be matched.
NODE EXPLANATION
----------------------------------------------------------------------
[*]{9,} any character of: '*' (at least 9 times
(matching the most amount possible))
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[^*]*? any character except: '*' (0 or more
times (matching the least amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
[*]{9,} any character of: '*' (at least 9 times
(matching the most amount possible))
----------------------------------------------------------------------
(?: group, but do not capture (0 or more times
(matching the most amount possible)):
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
[*]{9,} any character of: '*' (at least 9
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
. any character
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
^ the beginning of a "line"
----------------------------------------------------------------------
gaurav 'gaurav'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
\r '\r' (carriage return)
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
\Z before an optional \n, and the end of
the string
----------------------------------------------------------------------
) end of grouping
Java Code Example
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
public static void main(String[] asd){
String sourcestring = "source string to match with pattern";
Pattern re = Pattern.compile("[*]{9,}([^*]*?)[*]{9,}(?:(?![*]{9,}).)*^gaurav(?:\\r|\\Z)",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
Matcher m = re.matcher(sourcestring);
int mIdx = 0;
while (m.find()){
for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
}
mIdx++;
}
}
}
Returns
$matches Array:
(
[0] => Array
(
[0] => **********menu 1************
gaurav
)
[1] => Array
(
[0] => menu 1
)
)
Related
I'm trying to write a program which accepts any two numbers and checks if they are in the given pattern:
abb abbb abb abbb abbb abb abbb abbb abbb abb abbb abbb abbb abbb abb
I have put spaces for making it clearer. As you can see it follows a pattern in which everytime there is 'abb' followed by 'abbb', the number of times 'abbb' occurs in every iteration increases by 1. So it is something like this:
In every iteration:
abb(abbb++)
I'm trying to figure out how to do this using pattern library in Java but haven't got any solution yet.
You can use Regular expressions in java, read more on Regular expression API in java
import java.util.regex.Pattern;
System.out.println (Pattern.matches("abb(abbb)+","abbabbb"));
I understand the amount of abbb is incremented each time abb is met.
Then, validate with
^((abb|\1)abbb)+$
See regex proof.
Java code:
yourString.matches("((abb|\\1)abbb)+")
EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to \1 (1 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
abb 'abb'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
\1 what was matched by capture \1
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
abbb 'abbb'
--------------------------------------------------------------------------------
)+ end of \1 (NOTE: because you are using a
quantifier on this capture, only the LAST
repetition of the captured pattern will be
stored in \1)
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
If you meant to extract, use
abb(?:abbb)+
See this regex proof.
EXPLANATION
--------------------------------------------------------------------------------
abb 'abb'
--------------------------------------------------------------------------------
(?: group, but do not capture (1 or more times
(matching the most amount possible)):
--------------------------------------------------------------------------------
abbb 'abbb'
--------------------------------------------------------------------------------
)+ end of grouping
Java code:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "abb(?:abbb)+";
final String string = "abbabbb\nabbabbbabbabbbabbb\nabbabbbabbabbbabbbabbabbbabbbabbbabb\nabbabbbabbabbbabbb\n";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println(matcher.group(0));
}
}
}
I want to check the input string and validate if it is a correct number format and have written the following regex. [+-]?[0-9]*[.]?[0-9]*[e]?[+-]?[0-9]+. but unfortunately it is outputting true for --6 or ++6
import java.util.*;
public class Main {
public static void main(String args[]) throws Exception {
//Scanner
Scanner in = new Scanner(System.in);
int t = in.nextInt();
in.nextLine();
while(--t >= 0) {
String string = in.nextLine();
string = string.trim();
//System.out.println(string);
String regex = "[+-]?[0-9]*[.]?[0-9]*[e]?[+-]?[0-9]+";
//System.out.println(string.matches(regex));
if(string.matches(regex)) {
System.out.println(1);
}
else {
System.out.println(0);
}
}
}
}
This is matching due to the second [+-]? in your regex string. In ++6 or --6, it first matches the first +- since it is present, then again match the second +- since it was present, and then the digit.
But you were close. What you want to do is only match the second [+-]? if there is an exponent present. So just make the whole exponent part optional by enclosing in brackets and adding a ? at the end. Like this, you will match the second +- only if there is an e/E in front of it.
^[+-]?([0-9]+)?[.]?[0-9]*([eE][+-]?[0-9]+)?$
Regex Demo.
I would use this
^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$
Formatted
^
[+-]?
(?:
\d+
(?: \. \d* )?
|
\. \d+
)
(?: [eE] [+-]? \d+ )?
$
I have a string that contains the following date range format variations. I need to find and replace with hour precision using a single java regex pattern. The date range is variable. Can you come up with a regex for me?
String Example
published_date:{05/31/16.23:41:24-?}
published_date:{05/31/16.23:41:24-06/21/16.23:41:24}
Expected Results
published_date:{05/31/16.23:00:00-?}
published_date:{05/31/16.23:00:00-06/21/16.23:00:00}
Description
This regex will find substrings that look like date/time stamps like 05/31/16.23:41:24. It'll capture the date and hour portions of and allow you to replace the minutes and seconds with 00.
([0-9]{2}\/[0-9]{2}\/[0-9]{2}\.[0-9]{2}):[0-9]{2}:[0-9]{2}
Replace With: $1:00:00
Example
Live Demo
https://regex101.com/r/qK8bL7/1
Sample text
published_date:{05/31/16.23:41:24-?}
published_date:{05/31/16.23:41:24-06/21/16.23:41:24}
After Replacement
published_date:{05/31/16.23:00:00-?}
published_date:{05/31/16.23:00:00-06/21/16.23:00:00}
Explanation
NODE EXPLANATION
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
\/ '/'
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
\/ '/'
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
\. '.'
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
: ':'
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
: ':'
----------------------------------------------------------------------
[0-9]{2} any character of: '0' to '9' (2 times)
----------------------------------------------------------------------
Try this. ":[0-9]+:[0-9]+".
public class Regex {
public static void main(String ar[]){
String st = "05/31/16.23:41:24-06/21/16.23:41:24";
st = st.replaceAll(":[0-9]+:[0-9]+", ":00:00");
System.out.println(st);
st = "05/31/16.23:41:24-?";
st = st.replaceAll(":[0-9]+:[0-9]+", ":00:00");
System.out.println(st);
}
}
I have the following regular expression:
(.*)(?:([\+\-\*\/])(-?\d+(?:\.\d+)?))
the intention is to capture mathematical expressions in the form (left expression) (operator) (right operand), e.g. 1+2+3 would be captured as (1+2)(+)(3). It will also handle single operands, e.g. 1+2 would be captured as (1)(+)(2).
The problem I am having is that this regular expression won't match on a single operand with no operator, e.g. 5 should be matched in the first capture group with nothing in the second and third (5)()(). If I make the last part optional:
(.*)(?:([\+\-\*\/])(-?\d+(?:\.\d+)?))?
then the initial group will always capture the entire expression. Is there any way I can make the second part optional but have it take precedence over the greedy matching done by the first group?
Description
This Regex will:
captures the math expression upto the last operation
captures the last operation
captures the last number in the math expression
assumes that each number might have a plus or minus sign showing that the number is positive or negative
assumes each number might be non-integer
assumes the math expression can contain any number of operations such as: 1+2 or 1+2+3 or 1+2+3+4 or 1+2+3+4...
validates the string is a math expression. There are some edge cases which are not accounted for here such as the use of parenthesis, or other complex math symbols.
Raw Regular Expression
Note this being Java, you'll need to escape the back slashes in this regex. To escape them simply replace all the \ with a \\.
^(?=(?:[-+*/^]?[-+]?\d+(?:[.]\d+)?)*$)([-+]?[0-9.]+$|[-+]?[0-9.]+(?:[-+*/^][-+]?[0-9.]+)*(?=[-+*/^]))(?:([-+*/^])([-+]?[0-9.]+))?$
Explanation
Overview
In this expression I'm first validating that the string is composed of only operations -+/*^, optional signs -+, and integer or noninteger numbers. Since was already validated the rest of the expression can simply refer to numbers as [0-9.]+, which improves the readability.
Capture Groups
0 Gets the entire string
1 Gets the entire string upto but not including the last operation, if there are no operations then group 1 will have the entire string
2 Gets the last operation, if it exists
3 Gets the number and sign after the last operation
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
----------------------------------------------------------------------
[-+*/^]? any character of: '-', '+', '*', '/',
'^' (optional (matching the most
amount possible))
----------------------------------------------------------------------
[-+]? any character of: '-', '+' (optional
(matching the most amount possible))
----------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
[.] any character of: '.'
----------------------------------------------------------------------
\d+ digits (0-9) (1 or more times
(matching the most amount possible))
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
$ before an optional \n, and the end of
the string
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[-+]? any character of: '-', '+' (optional
(matching the most amount possible))
----------------------------------------------------------------------
[0-9.]+ any character of: '0' to '9', '.' (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
$ before an optional \n, and the end of
the string
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[-+]? any character of: '-', '+' (optional
(matching the most amount possible))
----------------------------------------------------------------------
[0-9.]+ any character of: '0' to '9', '.' (1 or
more times (matching the most amount
possible))
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
----------------------------------------------------------------------
[-+*/^] any character of: '-', '+', '*', '/',
'^'
----------------------------------------------------------------------
[-+]? any character of: '-', '+' (optional
(matching the most amount possible))
----------------------------------------------------------------------
[0-9.]+ any character of: '0' to '9', '.' (1
or more times (matching the most
amount possible))
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
[-+*/^] any character of: '-', '+', '*', '/',
'^'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
[-+*/^] any character of: '-', '+', '*', '/',
'^'
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
( group and capture to \3:
----------------------------------------------------------------------
[-+]? any character of: '-', '+' (optional
(matching the most amount possible))
----------------------------------------------------------------------
[0-9.]+ any character of: '0' to '9', '.' (1
or more times (matching the most
amount possible))
----------------------------------------------------------------------
) end of \3
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
Examples
Sample Text
1+2+-3
Sample Capture Groups
[0] = 1+2+-3
[1] = 1+2
[2] = +
[3] = -3
Online demo: http://fiddle.re/b2w5wa
Sample Text
-3
Sample Capture Groups
[0] = -3
[1] = -3
[2] =
[3] =
Online demo: http://fiddle.re/07kqra
Sample Java Code
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
public static void main(String[] asd){
String sourcestring = "source string to match with pattern";
Pattern re = Pattern.compile("^(?=(?:[-+*/^]?[-+]?\\d+(?:[.]\\d+)?)*$)([-+]?[0-9.]+$|[-+]?[0-9.]+(?:[-+*/^][-+]?[0-9.]+)*(?=[-+*/^]))(?:([-+*/^])([-+]?[0-9.]+))?$",Pattern.CASE_INSENSITIVE);
Matcher m = re.matcher(sourcestring);
int mIdx = 0;
while (m.find()){
for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
}
mIdx++;
}
}
}
I have input like this ==>
2 book at 12.99
4 potato chips at 3.99
I want to extract the numeric values from each line and store them in variables
for example in the line.. 2 book at 12.99 i want to extract Qauntity =2 and Price =12.99
from the given string
You can use:
Pattern p = Pattern.compile("(\\d+)\\D+(\\d+(?:.\\d+)?)");
Matcher mr = p.matcher("4 potato chips at 3.99");
if (mr.find()) {
System.out.println( mr.group(1) + " :: " + mr.group(2) );
}
OUTPUT:
4 :: 3.99
Regex
(\d+)[^\d]+([+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?)
Debuggex Demo
Description (Example)
/^(\d+)[^\d]+([+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?)$/gm
^ Start of line
1st Capturing group (\d+)
\d 1 to infinite times [greedy] Digit [0-9]
Negated char class [^\d] 1 to infinite times [greedy] matches any character except:
\d Digit [0-9]
2nd Capturing group ([+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?)
Char class [+-] 0 to 1 times [greedy] matches:
+- One of the following characters +-
Char class [0-9] 1 to 3 times [greedy] matches:
0-9 A character range between Literal 0 and Literal 9
(?:,?[0-9]{3}) Non-capturing Group 0 to infinite times [greedy]
, 0 to 1 times [greedy] Literal ,
Char class [0-9] 3 times [greedy] matches:
0-9 A character range between Literal 0 and Literal 9
(?:\.[0-9]{2}) Non-capturing Group 0 to 1 times [greedy]
\. Literal .
Char class [0-9] 2 times [greedy] matches:
0-9 A character range between Literal 0 and Literal 9
$ End of line
g modifier: global. All matches (don't return on first match)
m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
Capture Group 1: Contains the Quantity
Capture Group 2: Contains the Amount
Java
try {
Pattern regex = Pattern.compile("(\\d+)[^\\d]+([+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\\.[0-9]{2})?)", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
for (int i = 1; i <= regexMatcher.groupCount(); i++) {
// matched text: regexMatcher.group(i)
// match start: regexMatcher.start(i)
// match end: regexMatcher.end(i)
}
}
} catch (PatternSyntaxException ex) {
// Syntax error in the regular expression
}
Note: This java is just an example, I don't code in Java
You can use MessageFormat class. Below is the working example:
MessageFormat f = new MessageFormat("{0,number,#.##} {2} at {1,number,#.##}");
try {
Object[] result = f.parse("4 potato chips at 3.99");
System.out.print(result[0]+ ":" + (result[1]));
} catch (ParseException ex) {
// handle parse error
}