This piece of code is part of my programme and I am trying to print out the last integer value of the string only whenever the operator and the equals sign are together (e.g. ^=, *=, etc.).
Hence, if I enter 4 4 ^ 4 ^ 4 ^=, I would only want to print out "4". The same counts if the number 4 is directly before the "^=", e.g. 4 4 ^ 4 ^ 4^=.
My code is this:
if ((input.endsWith("^=")) | (input.endsWith("*=")) |
(input.endsWith("+=")) | (input.endsWith("-=")) |
(input.endsWith("%=")) | (input.endsWith("/=")))
{
Pattern p = Pattern.compile("[^\\d]*[\\d]+[^\\d]+([\\d]+)");
Matcher m = p.matcher(input);
if (m.find()) {
System.out.println(m.group(1)); // second matched digits
}
}
Currently my code prints out the number 4 multiple times, but I would only want to print it once. Any help is is appreciate.
Thank you!
You might use:
([0-9]+)\h*[\^+%/*-]=(?!.*[\^+%/*-]=)
([0-9]+) Capture 1+ digits 0-9 in group 1
\h* Match 0+ horizontal whitespace chars
[\^+%/*-]= Match any of the listed followed by =
(?!.*[\^+%/*-]=) Negative lookahead, assert what is on the right does not contain an operator followed by an equals sign
Regex demo | Java demo
In Java
final String regex = "([0-9]+)\\h*[\\^+%/*-]=(?!.*[\\^+%/*-]=)";
Try
(\d+)\s*[-+*/^%]=$
Find 1 or more digits and capture them
if they're followed by 0 or more spaces
followed by -, +, *, /, ^ or %
followed by =
followed by the end of the string
Related
I am trying to mask the CC number, in a way that third character and last three characters are unmasked.
For eg.. 7108898787654351 to **0**********351
I have tried (?<=.{3}).(?=.*...). It unmasked last three characters. But it unmasks first three also.
Can you throw some pointers on how to unmask 3rd character alone?
You can use this regex with a lookahead and lookbehind:
str = str.replaceAll("(?<!^..).(?=.{3})", "*");
//=> **0**********351
RegEx Demo
RegEx Details:
(?<!^..): Negative lookahead to assert that we don't have 2 characters after start behind us (to exclude 3rd character from matching)
.: Match a character
(?=.{3}): Positive lookahead to assert that we have at least 3 characters ahead
I would suggest that regex isn't the only way to do this.
char[] m = new char[16]; // Or whatever length.
Arrays.fill(m, '*');
m[2] = cc.charAt(2);
m[13] = cc.charAt(13);
m[14] = cc.charAt(14);
m[15] = cc.charAt(15);
String masked = new String(m);
It might be more verbose, but it's a heck of a lot more readable (and debuggable) than a regex.
Here is another regular expression:
(?!(?:\D*\d){14}$|(?:\D*\d){1,3}$)\d
See the online demo
It may seem a bit unwieldy but since a credit card should have 16 digits I opted to use negative lookaheads to look for an x amount of non-digits followed by a digit.
(?! - Negative lookahead
(?: - Open 1st non capture group.
\D*\d - Match zero or more non-digits and a single digit.
){14} - Close 1st non capture group and match it 14 times.
$ - End string ancor.
| - Alternation/OR.
(?: - Open 2nd non capture group.
\D*\d - Match zero or more non-digits and a single digit.
){1,3} - Close 2nd non capture group and match it 1 to 3 times.
$ - End string ancor.
) - Close negative lookahead.
\d - Match a single digit.
This would now mask any digit other than the third and last three regardless of their position (due to delimiters) in the formatted CC-number.
Apart from where the dashes are after the first 3 digits, leave the 3rd digit unmatched and make sure that where are always 3 digits at the end of the string:
(?<!^\d{2})\d(?=[\d-]*\d-?\d-?\d$)
Explanation
(?<! Negative lookbehind, assert what is on the left is not
^\d{2} Match 2 digits from the start of the string
) Close lookbehind
\d Match a digit
(?= Positive lookahead, assert what is on the right is
[\d-]* 0+ occurrences of either - or a digit
\d-?\d-?\d Match 3 digits with optional hyphens
$ End of string
) Close lookahead
Regex demo | Java demo
Example code
String regex = "(?<!^\\d{2})\\d(?=[\\d-]*\\d-?\\d-?\\d$)";
Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
String strings[] = { "7108898787654351", "7108-8987-8765-4351"};
for (String s : strings) {
Matcher matcher = pattern.matcher(s);
System.out.println(matcher.replaceAll("*"));
}
Output
**0**********351
**0*-****-****-*351
Don't think you should use a regex to do what you want. You could use StringBuilder to create the required string
String str = "7108-8987-8765-4351";
StringBuilder sb = new StringBuilder("*".repeat(str.length()));
for (int i = 0; i < str.length(); i++) {
if (i == 2 || i >= str.length() - 3) {
sb.replace(i, i + 1, String.valueOf(str.charAt(i)));
}
}
System.out.print(sb.toString()); // output: **0*************351
You may add a ^.{0,1} alternative to allow matching . when it is the first or second char in the string:
String s = "7108898787654351"; // **0**********351
System.out.println(s.replaceAll("(?<=.{3}|^.{0,1}).(?=.*...)", "*"));
// => **0**********351
The regex can be written as a PCRE compliant pattern, too: (?<=.{3}|^|^.).(?=.*...).
The regex can be written as a PCRE compliant pattern, too: (?<=.{3}|^|^.).(?=.*...).
It is equal to
System.out.println(s.replaceAll("(?<!^..).(?=.*...)", "*"));
See the Java demo and a regex demo.
Regex details
(?<=.{3}|^.{0,1}) - there must be any three chars other than line break chars immediately to the left of the current location, or start of string, or a single char at the start of the string
(?<!^..) - a negative lookbehind that fails the match if there are any two chars other than line break chars immediately to the left of the current location
. - any char but a line break char
(?=.*...) - there must be any three chars other than line break chars immediately to the right of the current location.
If the CC number always has 16 digits, as it does in the example, and as do Visa and MasterCard CC's, matches of the following regular expression can be replaced with an asterisk.
\d(?!\d{0,2}$|\d{13}$)
Start your engine!
Question-
1]String length is 2 to 5
2]String contains at least 1 char and maximum 3 char
3]Atleast one number
I want do using lookhead.
What i tried but not working
^(?=(.[a-z]){1,3})(?=.[0-9]).{2,5}$
Try using below regex
(?=([\\w\\W]{2,5}$))(?=(.*([0-9]){1,}.*))^[^A-Za-z]*[A-Za-z](?!(?:[^A-Za-z]*[A-Za-z]){3})
public static void main(String[] args) {
String regex = "(?=([\\w\\W]{2,5}$))(?=(.*([0-9]){1,}.*))^[^A-Za-z]*[A-Za-z](?!(?:[^A-Za-z]*[A-Za-z]){3}).*";
Pattern pattern = Pattern.compile(regex);
System.out.println(pattern.matcher("AB2").find()); // output true
System.out.println(pattern.matcher("AB2C").find()); // output true
}
You might use a positive lookahead to assert that the length of the string is 2-5 characters and that there is at least 1 digit and 1 character [a-z]. To make sure that there is a maximum of 3 characters to can use a negative lookahead to assert that you don't match a character 4 times.
^(?=[a-z0-9]{2,5}$)(?=.*[a-z])(?=.*[0-9])(?!(?:.*[a-z]){4})[a-z0-9]+$
Regex demo
Explanation
^ Assert the start of the line
(?=[a-z0-9]{2,5}$ Positive lookahead to assert 2 -5 characters
(?=.*[a-z]) Positive lookahead to assert 1 character
(?=.*[0-9]) Positive lookahead to assert 1 digit
(?!(?:.*[a-z]){4}) Negative lookahead to not match a character 4 times
[a-z0-9]+$ Match one or more times a character or a digit until the end of the string
Demo
I have this regex in java
String pattern = "(\\s)(\\d{2}-)(enero|febrero|marzo|abril|mayo|junio|julio|agosto|septiembre|octubre|noviembre|diciembre)(-\\d{4})(\\s)";
It works as intended but I have a new problem to get some valid dates:
1st problem:
If I have this String It was at 22-febrero-1999 and 10-enero-2009 and 01-diciembre-2000 I should get another string as febrero-enero-diciembre and I only get febrero-enero
2nd problem
If I have a single date in a String like 12-octubre-1989 I get an emptry String.
Why I have in my pattern to have whitespaces in the start and end of any date? because I have to catch only valid months in a String like adsadasd 12-validMonth-2999 asd 11-validMonth-1989 I should get both validMonth, then never get a validMonth in a String like asdadsad12-validMonth-1989 asdadsad 23-validMonth-1989 in the last one I only should get the last validMonth
PD: My java code is
String resultado = "";
String pattern = "(\\s)(\\d{2}-)(enero|febrero|marzo|abril|mayo|junio|julio|agosto|septiembre|octubre|noviembre|diciembre)(-\\d{4})(\\s)";
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(fecha);
while (m.find()) {
resultado += m.group().split("-")[1] + "-";
}
return (resultado.compareTo("") == 0 ? "" : resultado.substring(0, resultado.length() - 1));
You might want to use a word boundary instead:
\\b(\\d{2}-)(enero|febrero|marzo|abril|mayo|junio|julio|agosto|septiembre|octubre|noviembre|diciembre)(-\\d{4})\\b
And I believe some of the months can be optimized a little bit (it could reduce readability unfortunately, but should speed things up by a notch):
\\b(\\d{2}-)((?:en|febr)ero|ma(?:rz|y)o|abril|ju[ln]io|agosto|(?:septiem|octu|noviem|diciem)bre)(-\\d{4})\\b
Perhaps try using a \b instead of \s:
String pattern = "\\b(\\d{2}-)(enero|febrero|marzo|abril|mayo|junio|julio|agosto|septiembre|octubre|noviembre|diciembre)(-\\d{4})\\b";
This will only match strings where the first digit is not preceded by another word character (digit, letter, or underscore), and the last digit is not followed by a word character. I've also removed the capturing groups around the \b, because it would always be a zero-length string, if matched.
I wouldn't use a word boundry as a delimeter.
I'd suggest to use either whitespace or NOT digit,
or no delimeter and put in a validation range of numbers for day/year.
This way you may catch more embeded dates that are in close
proximity (adjacent) to letters and underscore.
Something like:
# "(?<!\\d)\\d{2}-(?:enero|febrero|marzo|abril|mayo|junio|julio|agosto|septiembre|octubre|noviembre|diciembre)-\\d{4}(?!\\d)"
(?<! \d ) # Not a digit before us
\d{2} - # Two digits followed by dash
(?: # A month
enero
| febrero
| marzo
| abril
| mayo
| junio
| julio
| agosto
| septiembre
| octubre
| noviembre
| diciembre
)
- \d{4} # Dash followed by four digits
(?! \d ) # Not a digit after us
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
}
Ok... I have an unsatisfactory solution to a problem.
The problem is I have input like so:
{sup 19}F({sup 3}He,t){sup 19}Ne(p){sup 18}F
and need output like so:
¹⁹F(³He,t)¹⁹Ne(p)¹⁸F
I use a series of replacements first to split each of the {sup xx} sections into {sup x}{sup x} and then use a regex to match each of those and replace the characters with their UTF-8 single equivalents. The "problem" is that the {sup} sections can have numbers 1, 2 or 3 digits long (maybe more, I don't know), and I want to "expand" them into separate {sup} sections with one digit each. ( I also have the same problem with {sub} for subscripts... )
My current solution looks like this (in java):
retval = retval.replaceAll("\\{sup ([1-9])([0-9])\\}", "{sup $1}{sup $2}");
retval = retval.replaceAll("\\{sup ([1-9])([0-9])([0-9])\\}", "{sup $1}{sup $2}{sup $3}");
My question: is there a way to do this in a single pass no matter how many digits ( or at least some reasonable number ) there are?
Yes, but it may be a bit of a hack, and you'll have to be careful it doesn't overmatch!
Regex:
(?:\{sup\s)?(\d)(?=\d*})}?
Replacement String:
{sup $1}
A short explanation:
(?: | start non-capturing group 1
\{ | match the character '{'
sup | match the substring: "sup"
\s | match any white space character
) | end non-capturing group 1
? | ...and repeat it once or not at all
( | start group 1
\d | match any character in the range 0..9
) | end group 1
(?= | start positive look ahead
\d | match any character in the range 0..9
* | ...and repeat it zero or more times
} | match the substring: "}"
) | stop negative look ahead
} | match the substring: "}"
? | ...and repeat it once or not at all
In plain English: it matches a single digit, only when looking ahead there's a } with optional digits in between. If possible, the substrings {sup and } are also replaced.
EDIT:
A better one is this:
(?:\{sup\s|\G)(\d)(?=\d*})}?
That way, digits like in the string "set={123}" won't be replaced. The \G in my second regex matches the spot where the previous match ended.
The easiest way to do this kind of thing is with something like PHP's preg_replace_callback or .NET's MatchEvaluator delegates. Java doesn't have anything like that built in, but it does expose the lower-level API that lets you implement it yourself. Here's one way to do it:
import java.util.regex.*;
public class Test
{
static String sepsup(String orig)
{
Pattern p = Pattern.compile("(\\{su[bp] )(\\d+)\\}");
Matcher m = p.matcher(orig);
StringBuffer sb = new StringBuffer();
while (m.find())
{
m.appendReplacement(sb, "");
for (char ch : m.group(2).toCharArray())
{
sb.append(m.group(1)).append(ch).append("}");
}
}
m.appendTail(sb);
return sb.toString();
}
public static void main (String[] args)
{
String s = "{sup 19}F({sup 3}He,t){sub 19}Ne(p){sup 18}F";
System.out.println(s);
System.out.println(sepsup(s));
}
}
result:
{sup 19}F({sup 3}He,t){sub 19}Ne(p){sup 18}F
{sup 1}{sup 9}F({sup 3}He,t){sub 1}{sub 9}Ne(p){sup 1}{sup 8}F
If you wanted, you could go ahead and generate the superscript and subscript characters and insert those instead.
Sure, this is a standard Regular Expression construct. You can find out about all the metacharacters in the Pattern Javadoc, but for your purposes, you probably want the "+" metacharacter, or the {1,3} greedy quantifier. Details in the link.