I am using Java and I need to validate a numeric sequence like this: 9999/9999.
I tried using this regex \\d{4}\\\\d{4}, but I am getting false for matches().
My code:
Pattern regex = Pattern.compile("\\d{4}\\\\d{4}");
if (!regex.matcher(mySequence).matches()) {
System.out.println("invalid");
} else {
System.out.println("valid");
}
Can anyone help me, please?
The regex pattern is attempting to match a backslash rather than a forward slash character. You need to use:
Pattern regex = Pattern.compile("\\d{4}/\\d{4}")
Pattern regex = Pattern.compile("\\d{4}\\\\d{4}");
should be
Pattern regex = Pattern.compile("\\d{4}/\\d{4}");
Change your pattern to :
Pattern regex = Pattern.compile("\\d{4}\\\\\\d{4}");
for matching "9999\\9999" (actual value: 9999\9999) (in java you need to escape while declaring String)
or if you want to match "9999/9999" then above solutions would work fine:
Pattern regex = Pattern.compile("\\d{4}/\\d{4}");
Related
I'm using the following regex:
(?<=<((Pswrd>)|([^/]{1,2147483646}?:Pswrd>)))((?s).+?)(?=</(\\1))
And I have the following text to match:
<abc:Pswrd>PASSWORD_ONE</abc:Pswrd>
<Pswrd>PASSWORD_TWO</Pswrd>
I need to match the context of both XML tags but is only working for the second one.
The output is:
PASSWORD_TWO
And it should be:
PASSWORD_ONE
PASSWORD_TWO
It seems the OR is not working for some reason?
String message = " <abc:Pswrd>PASSWORD_ONE</abc:Pswrd>\n" +
" <Pswrd>PASSWORD_TWO</Pswrd>";
String regex = "(?<=<((Pswrd>)|([^/]{1,2147483646}?:Pswrd>)))((?s).+?)(?=</(\\1))";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(message);
while (matcher.find()) {
String group = matcher.group();
System.out.println(group);
}
Thanks
Update: It needs to be the matching group 0.
So in order to either match <Pswd> or <abc:Pswd> or <something:Pswd>, the RegEx would need to look something like <\w*:*Pswrd>. The problem however is that the look behind does not like non-fixed width quantifiers, so you can't create a look behind that caters for a "dynamic"
Instead I would suggest just go for something simple, such as :
(?<=Pswrd>)(.*)(?=<\/)
Essentially here you just look for the last bit of the opening tag (namely "Pswrd>") then you match any thing between that and the closing portion of the tag.
I crawl a site and i get some prices from it. I get a price with its currency (21,00 TL) i should remove currency(TL) and the left whitespace on it for convert double to string. In short i should get 21.00 . Whatevery i did , i couldnt remove that whitespace.
I got from crawler :
<b>21,00 TL</b>
What i try:
price_lower_str = price_lower_str.replace(" TL","");
and
price_lower_str = price_lower_str.replace(" TL","");
price_lower_str = price_lower_str.replace("TL","");
price_lower_str = price_lower_trim();
but i couldnt get only 21.00 . Who can help me?
Thanks
Quick and dirty, but working :-)
public static void main(String[] args) {
String str = "<b>21,00 TL</b>";
Matcher matcher = Pattern.compile(".*?([\\d]+,[\\d]+).*").matcher(str);
if (matcher.matches()) System.out.println(matcher.group(1).replace(',', '.'));
}
OUTPUT:
21.00
You're just using the wrong regular expression. Try this:
price_lower_str.replaceAll("(\\ |\\s)+TL", "")
First, I'm using replaceAll and not just replace as you are. Second, notice the parens - I'm replacing EITHER OR \s which matches any whitespace character. Finally, I'm escaping via backslashes the ampersand in Escaping backslashes when backslash itself is a meta-character in regex is a pain, but welcome to java regex.
Using regexes sound to heavy for this simple processing. It's not really efficient in that case. What you could do is to locate the > from the < b > tag and do a substring up to the amperstand.
System.out.println(test.substring(test.indexOf(">")+1, test.indexOf("&")));
You will get your answer 21,00
Just in an attempt to get more experience with regex (while also making life easier at work) I was trying to parse some filenames in Java.
My string is this: /home/user/example/Results/ExampleFilePrefix_20140324-0500_OptionalTextThatMightContainNumbers123.csv
basically the filename will always start with ExampleFilePrefix_ followed by the timestamp, and sometimes ends with OptionalTextThatMightContainNumbers123 just depending on how the file was generated. The relevant information I want is the timestamp followed by the optional text if it exists.
I was messing around with various regular expressions and while I can get them all to work with a Ruby regex parser I can't get any of them to work in Java. I didn't keep track of them as I went, but this is my most recent attempt:
_(\w+-\w+)
Which works as expected in Ruby: http://rubular.com/r/K2BiboURRo, but doesn't even come close to matching in Java: http://fiddle.re/c7m04
I don't think it's a problem the code I've written due to the fact the online parser doesn't match, but I'll paste it here to be sure.
private String extractFileName(String filename) {
String resultNameBase = "RegexDidntMatch";
Pattern pattern = Pattern.compile("_(\\w+-\\w+)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(filename);
if (matcher.matches() && matcher.find()) {
resultNameBase = matcher.group(1);
}
return resultNameBase;
}
As always, thanks to all in advance
First of of its only matcher.find() And the catch the group 0 instead of 1.
if (matcher.find()) {
resultNameBase = matcher.group();
}
This part is problem:
if (matcher.matches() && matcher.find())
Matcher#matches() matches complete input string with your regex.
Replace that with:
if (matcher.find())
Codes:
String etr = "fdsfsdaf\nTKT:1101234567890FSDFD";
the format is like ".+\nTKT:\d{13}.+".
how to get first 3 numberic after "\nTKT:" using String.replaceAll to implement? In this example, I want to get is "110". Exclude Matcher,Pattern and Other String method like indexOf(). Because I found the Regex include "?:","?<",but I have tried but failed,something like":
replaceAll(".+\nTKT:(?=[\\d{3}])\\d{10}.+","");
Thanks in advance.I just know some basic use for Regex.
Or you can replaceAll(".+\nTKT:(\\d{3})\\d{10}.+", "$1");
Instead of replacing,you should match it
Matcher m=Pattern.compile(".+\nTKT:(\\d{3})\\d{10}.+").matcher(input);
if(m.find())
{
m.group(1);
}
Hello I have this regex in Javascript :
var urlregex = new RegExp("((www.)(([a-zA-Z0-9-]){2,}\.){1,4}([a-zA-Z]){2,6}(\/([a-zA-Z-_\/\.0-9#:?=&;,]*)?)?)");
And when I try to put it on a Java String I have this error :
Description Resource Path Location Type
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ ) CreateActivity.java /SupLink/src/com/supinfo/suplink/activities line 43 Java Problem
So I just want to know what I have to change to render it in Java in order to do this(this function runs fine) :
private boolean IsMatch(String s, String pattern)
{
try {
Pattern patt = Pattern.compile(pattern);
Matcher matcher = patt.matcher(s);
return matcher.matches();
} catch (PatternSyntaxException e){
return false;
}
}
EDIT :
Thank you for your help, now I have this :
private String regex = "((www.)(([a-zA-Z0-9-]){2,}\\.){1,4}([a-zA-Z]){2,6}(\\/([a-zA-Z-_\\/\\.0-9#:?=&;,]*)?)?)";
But I don't match what I really want (regex are horrible ^^), I would like to match thes types of urls :
www.something.com
something.com
something.com/anotherthing/anything
Can you help me again ?
really thanks
When you create the Java string, you need to escape the backslashes a second time so that Java understands that they are literal backslashes. You can replace all existing backslashes with \\. You also need to escape any Java characters that normally need to be escaped.
Currently your regex require www at start. If you want to make it optional add ? after (www.). Also you probably want to escape . after www part. Your regex should probably look like.
"((www\\.)?(([a-zA-Z0-9-]){2,}\\.){1,4}([a-zA-Z]){2,6}(\\/([a-zA-Z-_\\/\\.0-9#:?=&;,]*)?)?)"
You should scape \
something like this
"((www.)(([a-zA-Z0-9-]){2,}\\.){1,4}([a-zA-Z]){2,6}(\\/([a-zA-Z-_\\/\\.0-9#:?=&;,]*)?)?)"