java assignment regex - java

Could you please provide regex to match assignments in the text
$one: 3-2; $three: 4-1; 4
$one: 3-2
I've tried \$\w+:.+?;? and expect it to match $one: 3-2; and $one: 3-2.
But it matches only $one:
What am I missing?

Think you mean this,
\$\w+:[^;]*;?
In java.
"\\$\\w+:[^;]*;?"
Your regex matches upto the first space it's mainly because of the non-greedy pattern and the following optional semicolon.

\$\w+:.+?(?:;|$)
You need to use this with multi line mode. Yours is not working because ; is optional and .+? is non-greedy so it will stop by consuming only 1 character as it has the option to ignore ;
See demo:
https://regex101.com/r/eX9gK2/8

You can use this regex:
\$\w+:\s*([^;]+)
RegEx Demo
In Java it will be:
"\\$\\w+:\\s*([^;]+)"

Related

Regex Pattern in Java

I have a regular expression as defined
AAA_BBB_CCCC_(.*)_[0-9][0-9][0-9][0-9][0-1][0-9][0-3][0-9]T[0-2][0-9][0-5][0-9][0-5][0-9].
There is a string defined as --> **AAA_BBB_CCCC_DDD_EEEE_19710101T123456** and in the code, we have matcher.group(1) which can filter out what is desired as (DDD_EEEE). Now, I've a new string coming in as --> **AAA_BBB_ATCCCC_DDD_EEEE_19710101T123456**. Is there a way that I can change the regex to satisfy both old and new string? I tried few solutions that came up from Stackoverflow questions like this and others but that didn't work quite right for me.
You just need to add an optional group, (?:AT)?, before CCCC:
AAA_BBB_(?:AT)?CCCC_(.*)_[0-9]{4}[0-1][0-9][0-3][0-9]T[0-2][0-9][0-5][0-9][0-5][0-9]
^^^^^^^
See the regex demo
I also contracted the four [0-9] to [0-9]{4} to make the pattern shorter.
The (?:AT)? is a non-capturing group to which a ? quantifier is applied. The ? quantifier makes the whole sequence of letters match 1 or 0 times, making it optional in the end.
Please give the following regex a try.
AAA_BBB_(ATCCCC|CCCC)_(.*)_[0-9][0-9][0-9][0-9][0-1][0-9][0-3][0-9]T[0-2][0-9][0-5][0-9][0-5][0-9].
It would only match ATCCCC or CCCC. It won't be able to support dynamic characters preceding CCCC. You would need to use wildcards for that.
Also, you would need to change your matcher.group(1) statement to matcher.group(2)

Regex for emoticon

have this regex:
(:?^|\s)+(;\))+
Im tryng to capture all ocurrences of ;) if it appears alone (between spaces) or at start of line.
Valid examples
;)
;)
;) ;) -> Should be 2 groups of ;)
Dont allow
a;)a
a;)
;)a
Current regex just captures first group for ;) ;) case because second ;) is expecting for a space but its used by first group..
You can match ;) using lookarounds:
(?<=\s|^);\)(?=\s|$)
RegEx Demo
(?<=\s|^) is look behind that asserts line start or a whitespace is at previous position
(?=\s|$) is look ahead that matches line end or a whitespace is at next position
In Java:
Pattern p = Pattern.compile("(?<=\\s|^);\\)(?=\\s|$)");
I'd like to suggest a more concise lookaround based solution:
String rx = "(?x)(?<!\\S) ;\\) (?!\\S)";
See the regex demo
Explanation:
(?x) - COMMENTS modifier ensuring that all pattern whitespace is ignored, and embedded comments starting with # are ignored until the end of a line (so that we can better see the parts of the pattern)
(?<!\\S) - a negative lookbehind failing the match if a ;) is preceded with a non-whitespace char
;\\) - literal ;)
(?!\\S) - a negative lookahead that fails the match if there is a non-whitespace char after the ;).
See the Java demo with a replaceAll to show it finds only those ;) you need:
String s = ";)\n ;) \n;) ;) -> Should be 2 groups of ; )\n\nDont allow\na;)a\na;) \n;)a";
System.out.println(s.replaceAll("(?x)(?<!\\S) ;\\) (?!\\S)", "<found>$0</found>"));
If you want to further enhance the pattern and you do not feel comfortable with the COMMENTS modifier, remove it. Use "(?<!\\S);\\)(?!\\S)" then.

Regex to match \a574322 in Java

I have long string looking like this: \c53\e59\c9\e28\c20140326\a4095\c8\c15\a546\c11 and I need to find expressions starting with \a and followed by digits. For example: \a574322
And I have no idea how to build it. I can't use:
Pattern p = Pattern.compile("\\a\\d*");
because \a is special character in regex.
When I try to group it like this:
Pattern p = Pattern.compile("(\\)(a)(\\d)*");
I get unclosed group error even though there is even number of brackets.
Can you help me with this?
Thank you all very much for solution.
You can use this regex:
\\\\a\\d+
Code Demo
Since in Java you need to double escape the \\ once for String and second time for regex engine.
You have to change your regex to:
Pattern p = Pattern.compile("(\\\\a\\d+)");
The regex is:
(\\a\d+)
The idea is to escape a backslash and then also escape the backslash for \a, and match digits too.
You need 4 \.
2 to indicate to regex that it is not a special character, but a plain \, and 2 for each to tell the Java String that these are not special characters either. So you need to represent it in code this way:
"\\\\a\\d*"
Which is actually the regex \\a\d*
\\(a)[0-9]+ this should work
you can't try your regexps on this page or some similar
http://regex101.com/

Word that matches ^.*(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$

I am totally confused right now.
What is a word that matches: ^.*(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$
I tried at Regex 101 this 1Test#!. However that does not work.
I really appreciate your input!
What happens is that your regex seems to be in Java-flavor (Note the \\d)
that is why you have to convert it to work with regex101 which does not work with jave (only works with php, phyton, javascript)
see converted regex:
^.*(?=.*\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$
which will match your string 1Test#!. Demo here: http://regex101.com/r/gE3iQ9
You just want something that matches that regex?
Here:
a1a!
This pattern matches
\dTest#!
if u want a pattern which matches 1Test#! try this pattern
^.(?=.\d)(?=.[a-zA-Z])(?=.[!##$%^&]).*$
Your java string ^.*(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$ encodes the regexp expression ^.*(?=.*\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$.
This is because the \ is an escape sequence.
The latter matches the string you specified.
If your original string was a regexp, rather than a java string, it would match strings such as \dTest#!
Also you should consider removing the first .*, doing so would make the regexp more efficient. The reason is that regexp's by default are greedy. So it will start by matching the whole string to the initial .*, the lookahead will then fail. The regexp will backtrack, matchine the first .* to all but the last character, and will fail all but one of the loohaheads. This will proceed until it hits a point where the different lookaheads succeed. Dropping the first .*, putting the lookahead immidiately after the start of string anchor, will avoid this problem, and in this case the set of strings matched will be the same.

How do I use a regular expression to match an eight-digit number that can also have a space in the middle?

I want to match 1234 5678 or 12345678
Is this regular expression wrong?
if(!number.matches("^\\d{4}[, ]\\d{4}$")){
throw new Exception(" number is not valid : "+number);
}
try a quantifier after the []
^\d{4}[\s,]?\d{4}$
You are close, you need to specify that the space/comma is optional with a quantifier. ? is a good one because it means "zero or one". So your expression would be
^\d{4}[, ]?\d{4}$
Do you want to match the comma as well? [, ] matches a comma or a space char. The effect you're going for looks like ( |), except there are better ways to do it:
I think what you're looking for is:
/^\d{4}\s?\d{4}$/
Note that the \s can match any space char, including newlines. If you only want to match the space char ' ', then use the following:
/^\d{4}[ ]?\d{4}$/
there is modifcation required to match '12345678'
use this regular expression : "^\d{4}[, ]?\d{4}$"
Hope this helps
^\d{4}[\s]?\d{4}$
Try this without the comma.
Perhaps this is a good time to bring up the online Regex interactive tutorial.
Great tool for playing around with Regex expressions without having to mess up your own code.

Categories