Regex to update date precision - java

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);
}
}

Related

Replace specific character sequence after specific character

I have the folowing string for example:
O > O §o TEXT §r §o TEXT §r
I need to replace all §r with §r§a only after > character.
It should be
O > O §o TEXT §r§a §o TEXT §r§a
as the result.
I tried >*(\§r) regex but it ignores >.
May You point on my error?
The easiest way to do this would be to split it into two strings first and then run a replace. That is, you could take
int index = inputString.indexOf('>') + 1;
String first = inputString.subString(0, index);
String second = inputString.subString(index);
String finalString = first + second.replace("§r", "§r§a");
Doing this with a pure regular expression would be difficult.
Description
((?:(?!>).)*>.*?|)(§r)
Replace With: $1§r§a
** To see the image better, simply right click the image and select view in new window
Example
Live Demo
https://regex101.com/r/xP8dI5/1
Sample text
§r O > O §o TEXT §r §o TEXT §r
After Replacment
§r O > O §o TEXT §r§a §o TEXT §r§a
Explanation
NODE EXPLANATION
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
----------------------------------------------------------------------
(?! look ahead to see if there is not:
----------------------------------------------------------------------
> '>'
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
. any character except \n
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
> '>'
----------------------------------------------------------------------
.*? any character except \n (0 or more times
(matching the least amount possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
( group and capture to \2:
----------------------------------------------------------------------
§r '§r'
----------------------------------------------------------------------
) end of \2
----------------------------------------------------------------------
A pure regex way is by using \G like following
(\G(?!\A)|>)(.*?)§r
and, replace with
$1$2§r§a
Regex Demo
Java Code
System.out.println("O§r §r > O §o TEXT §r §o §r TEXT §r".replaceAll("(\\G(?!\\A)|>)(.*?)§r", "$1$2§r§a"));
Ideone Demo

pattern split to get all values in a string representing object

I have Strings that represent rows in a table like this:
{failures=4, successes=6, name=this_is_a_name, p=40.00}
I made an expression that can be used with Pattern.split() to get me back all of the values in a String[]:
[\{\,](.*?)\=
In the online regex tester it works well with the exception of the ending }.
But when I actually run the pattern against my first row I get a String[] where the first element is an empty string. I only want the 4 values (not keys) from each row not the extra empty value.
Pattern getRowValues = Pattern.compile("[\\{\\,](.*?)\\=");
String[] row = getRowValues.split("{failures=4, successes=6, name=this_is_a_name, p=40.00}");
//CURRENT
//row[0]=> ""
//row[1]=>"4"
//row[2]=>"6"
//row[3]=>"this_is_a_name"
//row[4]=>"40.00}"
//WANT
//row[0]=>"4"
//row[1]=>"6"
//row[2]=>"this_is_a_name"
//row[3]=>"40.00"
String[] parts = getRowValues
// Strip off the leading '{' and trailing '}'
.replaceAll("^\\{|\\}$", "")
// then just split on comma-space
.split(", ");
If you want just the values:
String[] parts = getRowValues
// Strip off the leading '{' and up to (but no including) the first =,
// and the trailing '}'
.replaceAll("^\\{[^=]*|\\}$", "")
// then just split on comma-space and up to (but no including) the =
.split(", [^=]*");
Option 1
Modify your regular expression to [{,](.*?)=|[}] where I removed all the unnecessarily escaped characters in each of the [...] constructs and added the |[}]
See also Live Demo
Option 2
=([^,]*)[,}]
This regular expression will do the following:
capture all the substrings after the = and before the , or close }
Example
Live Demo
https://regex101.com/r/yF2gG7/1
Sample text
{failures=4, successes=6, name=this_is_a_name, p=40.00}
Capture groups
Each match gets the following capture groups:
Capture group 0 gets the entire substring from = to , or }
Capture group 1 gets just the value not including the =, ,, or } characters
Sample Matches
[0][0] = =4,
[0][1] = 4
[1][0] = =6,
[1][1] = 6
[2][0] = =this_is_a_name,
[2][1] = this_is_a_name
[3][0] = =40.00}
[3][1] = 40.00
Explanation
NODE EXPLANATION
----------------------------------------------------------------------
= '='
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
[^,]* any character except: ',' (0 or more
times (matching the most amount
possible))
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
[,}] any character of: ',', '}'
----------------------------------------------------------------------

how to check a line start with foo or bar?

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

Regular Expression: match everything up to an optional capture group

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++;
}
}
}

Regular Expression (RegEx) for User Name in Java

How to form the RegEx of user name string in Java?
Rules in Exercise :
Only 3 - 10 characters.
Only 'a'-'z', 'A'-'Z', '1'-'9', '_' and '.' are allowed.
'_' and '.' can only be appeared 0 to 2 times.
"abc_._" = false
"abc..." = false
"abc__" = true
"abc.." = true
"abc_." = true
If I do not use Regex, it will be easier.
Without considering '1'-'9', I have tried the following RegEx but they are not workable.
String username_regex = "[a-zA-Z||[_||.]{0,2}]{3,10}";
String username_regex = "[a-zA-Z]{3,10}||[_||.]{0,2}";
My function :
public static boolean isUserNameCorrect(String user_name) {
String username_regex = "[a-zA-Z||[_]{0,2}]{3,10}";
boolean isMatch = user_name.matches(username_regex);
return isMatch;
}
What RegEx should I use?
If I remember well from CS classes, it is not possible to create one single regex to satisfy all three requirements. So, I would make separate checks for each condintion. For example, this regex checks for conditions 1 and 2, and condition 3 is checked separately.
private static final Pattern usernameRegex = Pattern.compile("[a-zA-Z1-9._]{3,10}");
public static boolean isUserNameCorrect(String userName) {
boolean isMatch = usernameRegex.matcher(userName).matches();
return isMatch && countChar(userName, '.')<=2 && countChar(userName, '_') <=2;
}
public static int countChar(String s, char c) {
int count = 0;
int index = s.indexOf(c, 0);
while ( index >= 0 ) {
count++;
index = s.indexOf(c, index+1);
}
return count;
}
BTW, notice the pattern that allows you to reuse a regex in Java (performace gain, because it is expensive to compile a regex).
The reason that a regex cannot do what you want (again if I remember well) is that this problem requires a context-free-grammar, while regex is a regular grammar. Ream more
First off, || isn't necessary for this problem, and in fact doesn't do what you think it does. I've only ever seen it used in groups for regex (like if you want to match Hello or World, you'd match (Hello|World) or (?:Hello|World), and in those cases you only use a single |.
Next, let me explain why each of the regex you have tried won't work.
String username_regex = "[a-zA-Z||[_||.]{0,2}]{3,10}";
Range operators inside a character class aren't interpreted as range operators, and instead will just represent the literals that make up the range operators. In addition, nested character classes are simply combined. So this is effectively equal to:
String username_regex = "[a-zA-Z_|.{0,2}]{3,10}";
So it'll match some combination of 3-10 of the following: a-z, A-Z, 0, 2, {, }, ., |, and _.
And that's not what you wanted.
String username_regex = "[a-zA-Z]{3,10}||[_||.]{0,2}";
This will match 3 to 10 of a-z or A-Z, followed by two pipes, followed by _, |, or . 0 to 2 times. Also not what you wanted.
The easy way to do this is by splitting the requirements into two sections and creating two regex strings based off of those:
Only 3 - 10 characters, where only 'a'-'z', 'A'-'Z', '1'-'9', '_' and '.' are allowed.
'_' and '.' can only appear 0 to 2 times.
The first requirement is quite simple: we just need to create a character class including all valid characters and place limits on how many of those can appear:
"[a-zA-Z1-9_.]{3,10}"
Then I would validate that '_' and '.' appear 0 to 2 times:
".*[._].*[._].*"
or
"(?:.*[._].*){0,2}" // Might work, might not. Preferable to above regex if easy configuration is necessary. Might need reluctant quantifiers...
I'm unfortunately not experienced enough to figure out what a single regex would look like... But these are at least quite readable.
May not be elegant but you may try this:
^(([A-Za-z0-9\._])(?!.*[\._].*[\._].*[\._])){3,10}$
Here is the explanation:
NODE EXPLANATION
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
( group and capture to \1 (between 3 and 10
times (matching the most amount
possible)):
--------------------------------------------------------------------------------
( group and capture to \2:
--------------------------------------------------------------------------------
[A-Za-z0-9\._] any character of: 'A' to 'Z', 'a' to
'z', '0' to '9', '\.', '_'
--------------------------------------------------------------------------------
) end of \2
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
.* any character except \n (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
[\._] any character of: '\.', '_'
--------------------------------------------------------------------------------
.* any character except \n (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
[\._] any character of: '\.', '_'
--------------------------------------------------------------------------------
.* any character except \n (0 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
[\._] any character of: '\.', '_'
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
){3,10} 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
This will satisfy your above-mentioned requirement. Hope it helps :)
Please try this:
[[a-Z][0-9][._]?[[a-Z][0-9][._]?[[a-Z][0-9]*
Niko
EDIT :
You're right. Then several Regexp :
Regex1: ^[\w.]{3-10}$
Regex2: ^[[a-Z][0-9]][_.]?[[a-Z][0-9]][_.]?[[a-Z][0-9]]*$
I hope I forgot nothing!

Categories

Resources