Java regex for String:number:String? - java

Hi i want to know about a regex for parsing
InputDispatcherTest.TimmyTest_ASSERT
customize/base/services/input/tests/InputDispatcher_test.cpp:153: Failure
i am using \\s[:]\\d[:]\\s but it's not working.

What kind of number? Integer? Floating point? What's your input?
That doesn't look right to me. I'd do this for integers:
^\\d+$
I'd recommend trimming the String before you check.

\s matches a whitespace character, Try:
\\w+[:]\\d+[:]\\w+

I think this would suffice :
".+:\\d+:.+"

"\S+[.]\S+:\d+:[ ]\S+"
I used above Regex as a solution

Related

Pattern matching in Java for alphabets and numbers

I am doing a simple pattern matching, which is not working. Please help
The string is:
The number *TER8347834SC* has problems.
The String contains a number TER8347834SC which may change with different messages, so i need to use regex to match this number while comparing the String. So while comparing String I am using the regex as [A-Z0-0] for TER8347834SC which doesn't match.
I know this is quite simple, but i tried many times, please help me in this.
Think you mean this,
"\\b[A-Z0-9]+\\b"
Note that \\b word boundary is a much needed one.
Try using this one:
([A-Z]+[0-9]+)
Your pattern should be like this ONLY if the message is always the same as you mentioned:
String pattern = "The number (.*) has problems.";

Regex to match slash does not work

I want a regex to match these:
/web/search/abc
/web/search/employee/999999999
/web/search/employee/78524152
But not this:
/web/search/employee/123456789
I wrote the following regex for Java, but it does not seem to be working.
/web/search/(?!/(employee/123456789)).*
Can someone tell me the correct regex to do this?
This is because you double the / in the lookahead. Try with:
/web/search/(?!employee/123456789$).*
What you tried is : /web/search/(?!/(employee/123456789)) can be represented as
You need to change it as /web/search/(?!employee/123456789) can be represented as
I would say
str.contains("^/web/search/(?!employee/123456789)")
satisfies your requirements.
Online demonstration here: http://regex101.com/r/dF1eU9
Try the following:
/web/search/(?!(employee/123456789$)).*
The slash was doubled in the negating look-ahead group.

Regular expression for format XXXXXXX_YZZZZ

I am trying to write a regular expression in java which will validate following format-
XXXXXXXX_YZZZZ
where
X – alphanumeric characters(8 characters)
Y - alpha character
Z - numeric characters
what I have tried for first part is - ^[a-zA-Z0-9 ]*$ but I am not getting how to go for second part.
Can any one tell me what will be the correct regex for required format ?
You forgot to specify the amount and the underscore I assume...
/^[a-z0-9]{8}_[a-z][0-9]{4}$/i
Look at JavaDoc, then you can translate your requirements to:
"^\\p{Alnum}{8}_\\p{Alpha}\\p{Digit}{4}$"
It uses predefined character classes, like you listed in your question.
How about this?
^[a-ZA-Z0-9]{8}\_[a-zA-Z][0-9]{4}$
You can also group the results:
^([a-ZA-Z0-9]{8})\_([a-zA-Z])([0-9]{4})$
so that you can address the X, Y and Z parts individually from the results.
Try this regex:
^[A-Za-z\d]{8}_[A-Za-z]\d{4}$
Your regex matches zero or more alphanumeric characters and/or whitespaces.
This is a good place to learn regex : http://www.regular-expressions.info
Try this regular expression
^[a-zA-Z0-9]{8}[_][a-zA-Z][0-9]{4}$
Try:
^[a-zA-Z0-9]{8}_[a-zA-Z][0-9]{4}$
Regexper is your friend here.
^[a-zA-Z0-9]{8}_[a-zA-Z][0-9]{4}$
In Java, you can use metacharacters to express regulars expressions :
"8abba778_a2012".matches("^\\w{8}_[a-z]\\d{4}$");
[EDIT] : According #Jon Dvorak, I am correcting my answer. In fact, \w is too generous and also applies to the underscore character _. The correct answer :
"8abba778_a2012".matches("^[a-zA-Z0-9]{8}_[a-z]\\d{4}$");

Eclipse regex for finding the following string

I want to search all files in my project in eclipse and replace all occurences of ConnectionID="lettersandnumbers"
what is the regex for the bit between the quotation marks(including the quotations)?
assuming you want to replace the String contents, look for
(ConnectionID=").*?(")
and replace with
$1replacement$2
where replacement is what you want to replace the String with :-)
Put this in your search box:
ConnectionID=\"[\w]+\"
Try this:
ConnectionID="[\p{L}\p{N}]*"
This will match all Unicode letters and numbers. Remember, à is a letter too.
I would search for:
ConnectionID="(.*)"
If you're not interested in keeping the chars between quotes you can use
ConnectionID=".*"

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