I have searched for hours using regular expression generators and checkers but i cant seem to get it to work...
I have this string: hdr("");cr(92);cl(3,"",4,"420720250","random message here");etr();
and so far my code is :
private void strchecker() {
Pattern pattern = Pattern.compile("(\\d{9})");
Matcher matcher = pattern.matcher(strLine);
if (matcher.find()) {
System.out.println(matcher.group(0)); //prints /{item}/
} else {
//System.out.println("Match not found");
}
}
This code is working and it finds the 9 digit number in the string. What im trying to do it find the regex code to search for "cl(3" or "cl(2", if it exists then send the 9 digit number to a variable. i just don't know how to find that cl(3 or 2..
any advice?
Thanks
Matt
/cl\([23].*(\d{9})/
The final parentheses will capture the 9 digits in group 1.
Since you note you're using javascript, I think you could do it like this, as you can't use lookbehind; you just have to grab the capture group rather than use the full match.
cl\\([23].*?(\\d{9})
Related
i am searching a way(regex expression) to get anything outside the brackets in java
like
String sample = "Hi all(igi)";
Output like
Hi all
Searched a lot but not able to find it on stack or google
What i am trying
Matcher m = Pattern.compile("(?:[^<>(^)]++)").matcher(abc);
while (m.find()) {
String newName = m.group(0);
System.out.println(newName);
}
It is giving me both text
also i want a regex expression not a workaround (i guess it can be done in regex only)
also explain the regex expression if you got the answer i want to learn how to achieve it
You can easily use replaceAll with some regex \(.*?\) which mean replace every thing between brackets so in the end you will get only the result that is not between the brackets :
sample = sample.replaceAll("\\(.*?\\)", "");
Example:
Input Output
Hi all(igi) Hi all
Hi all(igi) Some string Hi all Some string
I am having trouble with a regex in salesforce, apex. As I saw that apex is using the same syntax and logic as apex, I aimed this at java developers also.
I debugged the String and it is correct. street equals 'str 3 B'.
When using http://www.regexr.com/, the regex works('\d \w$').
The code:
Matcher hasString = Pattern.compile('\\d \\w$').matcher(street);
if(hasString.matches())
My problem is, that hasString.matches() resolves to false. Can anyone tell me if I did something somewhere wrong? I tried to use it without the $, with difference casing, etc. and I just can't get it to work.
Thanks in advance!
You need to use find instead of matches for partial input match as matches attempts to match complete input text.
Matcher hasString = Pattern.compile("\\d \\w$").matcher(street);
if(hasString.find()) {
// matched
System.out.println("Start position: " + hasString.start());
}
Hi guys I have problem getting the regex castling notation right.
O-O - King
O-O-O - Queen
I have the following code
Pattern castlingKPattern = Pattern.compile("O-O");
Matcher castlingKMatch = castlingKPattern.matcher(pgn);
Pattern castlingQPattern = Pattern.compile("O-O-O");
Matcher castlingQMatch = castlingQPattern.matcher(pgn);
if (castlingKMatch.find()){
System.out.println("Castling King: "+ pgn);
}
}else if (castlingQMatch.find()){
System.out.println("Castling Queen: "+ pgn);
}
But the following code keeps on going to the first if not to the second if statement. Even if I change the input to O-O-O which is supposed to go to CastlingQMatch or second if.
Can you please help. Thanks
Restrict the first regex to this:
"(?<!O-)O-O(?!-O)"
(?!-O) is negative lookahead that means fail the match if -O is next
(?!<O-) is negative lookbehind that means fail the match if O- is preceding text
RegEx Demo
The following regex works in the find dialog of Eclipse but throws an exception in Java.
I can't find why
(?<=(00|\\+))?[\\d]{1}[\\d]*
The syntax error is at runtime when executing:
Pattern.compile("(?<=(00|\\+))?[\\d]{1}[\\d]*")
In the find I used
(?<=(00|\+))?[\d]{1}[\d]*
I want to match phone numbers with or without the + or 00. But that is not the point because I get a Syntax error at position 13. I don't get the error if I get rid of the second "?"
Pattern.compile("(?<=(00|\\+))[\\d]{1}[\\d]*")
Please consider that instead of 1 sometime I need to use a greater number and anyway the question is about the syntax error
If your data looks like 00ddddd or +ddddd where d is digit you want to get #Bergi's regex (?<=00|\\+)\\d+ will do the trick. But if your data sometimes don't have any part that you want to ignore like ddddd then you probably should use group mechanism like
String[] data={"+123456","00123456","123456"};
Pattern p=Pattern.compile("(?:00|\\+)?(\\d+)");
Matcher m=null;
for (String s:data){
m=p.matcher(s);
if(m.find())
System.out.println(m.group(1));
}
output
123456
123456
123456
Here is an example that works for me:
public static void main(String[] args) {
Pattern pattern = Pattern.compile("(?<=00|\\+)(\\d+)");
Matcher matcher = pattern.matcher("+1123456");
if (matcher.find()) {
System.out.println(matcher.group(1));
}
}
You might shorten your regex a lot. The character classes are not needed when there is only one class inside - just use \d. And {1} is quite useless as well. Also, you can use + for matching "one or more" (it's short for {1,}). Next the additional grouping in your lookbehind should not be needed.
And last, why is that lookbehind optional (with ?)? Just leave it away if you don't need it. This might even be the source of your pattern syntax error - a lookaround must not be optional.
Try this:
/(?<=00|\+)\d+/
Java:
"(?<=00|\\+)\\d+"
I have a string named s_Result which will be parsed from the Internet. The format may be "Distance: 2.8km (about 9 mins)", and there are 4 variables which are f_Distance, m_DistanceUnit, f_timeEst, m_timeEstUnit.
My question is how to parse s_Result and assign 2.8, km, 9, mins into f_Distance, m_distanceUnit, f_timeEst and m_timeEstUnit respectively using regular expression?
I tried using "\d+(\.\d+)?" in RegEx Tester and the result showed 2 matches found, but if I use "\\d+(\\.\\d+)?" in Android code, it showed no matches!
Any suggestions what might be going wrong?
Something like that?
String s_Result="Distance: 2.8km (about 9 mins)";
//Distance parsing
Pattern p = Pattern.compile("Distance: (\\d+(\\.\\d+)?)(.*?)\\b");
Matcher m = p.matcher(s_Result);
if(m.find()){
MatchResult mr=m.toMatchResult();
f_Distance=mr.group(1);//2.8
m_DistanceUnit=mr.group(3);//km
}
//Time parsing
p = Pattern.compile("about (\\d+(\\.\\d+)?) (.*)\\b");
m = p.matcher(s_Result);
if(m.find()){
MatchResult mr=m.toMatchResult();
f_timeEst=mr.group(1);//9
m_timeEstUnit=mr.group(3);//min
}
And here's another option for you, to match more flexible format:
String s_Result="Distance: 2.8km (about 9 mins)";
Pattern p = Pattern.compile("(\\d+(\\.\\d+)?) ?(\\w+?)\\b");
Matcher m = p.matcher(s_Result);
while(m.find()){
MatchResult mr=m.toMatchResult();
String value=mr.group(1);//2.8 and 9 come here
String units=mr.group(3);//km and mins come here
}
I tried using "\d+(\.\d+)?" in RegEx Tester and the result showed 2 matches found, but if I use "\\d+(\\.\\d+)?" in Android code, it showed no matches!
Probably you was using String#matches() or Matcher#matches() which would match on the entire string. The regex is then actually "^\\d+(\\.\\d+)?$". Better use Matcher#find() instead.
String s = "Distance: 2.8km (about 9 mins)";
Matcher m = Pattern.compile("\\d+(\\.\\d+)?").matcher(s);
while (m.find()) {
System.out.println(m.group(0)); // 2.8 and 9
}
That said, have you considered writing a parser?
It's absolutely the same as in Java.
Really, most of Android questions are not related to Android itself, but to Java programming in general. Please try to search for more general questions before posting new 'Android' questions.