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/
Related
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.
When I use the following code I've got an error:
Matcher matcher = pattern.matcher("/Date\(\d+\)/");
The error is :
invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
I have also tried to change the value in the brackets to('/Date\(\d+\)/'); without any success.
How can i avoid this error?
You need to double-escape your \ character, like this: \\.
Otherwise your String is interpreted as if you were trying to escape (.
Same with the other round bracket and the d.
In fact it seems you are trying to initialize a Pattern here, while pattern.matcher references a text you want your Pattern to match.
Finally, note that in a Pattern, escaped characters require a double escape, as such:
\\(\\d+\\)
Also, as Rohit says, Patterns in Java do not need to be surrounded by forward slashes (/).
In fact if you initialize a Pattern like that, it will interpret your Pattern as starting and ending with literal forward slashes.
Here's a small example of what you probably want to do:
// your input text
String myText = "Date(123)";
// your Pattern initialization
Pattern p = Pattern.compile("Date\\(\\d+\\)");
// your matcher initialization
Matcher m = p.matcher(myText);
// printing the output of the match...
System.out.println(m.find());
Output:
true
Your regex is correct by itself, but in Java, the backslash character itself needs to be escaped.
Thus, this regex:
/Date\(\d+\)/
Must turn into this:
/Date\\(\\d+\\)/
One backslash is for escaping the parenthesis or d. The other one is for escaping the backslash itself.
The error message you are getting arises because Java thinks you're trying to use \( as a single escape character, like \n, or any of the other examples. However, \( is not a valid escape sequence, and so Java complains.
In addition, the logic of your code is probably incorrect. The argument to matcher should be the text to search (for example, "/Date(234)/Date(6578)/"), whereas the variable pattern should contain the pattern itself. Try this:
String textToMatch = "/Date(234)/Date(6578)/";
Pattern pattern = pattern.compile("/Date\\(\\d+\\)/");
Matcher matcher = pattern.matcher(textToMatch);
Finally, the regex character class \d means "one single digit." If you are trying to refer to the literal phrase \\d, you would have to use \\\\d to escape this. However, in that case, your regex would be a constant, and you could use textToMatch.indexOf and textToMatch.contains more easily.
To escape regex in java, you can also use Pattern.quote()
I know this a duplicate of a question with almost the identical name, however, I can't get it to work in Android what so ever!
I am trying this: Regex to Match Symbols:
public Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\\";'<>?,.\/]");
However, this isn't working. Does anyone know the correct method of applying this pattern?
P.S. Complete noob at Regex. :D
From here originally - Regex to Match Symbols: !$%^&*()_+|~-=`{}[]:";'<>?,./
Error Message: Syntax error on token(s), misplaced construct(s)
UPDATE: Added extra backslashes...fixed a lot of em, now gets error from ; onwards. Using Eclipse.
I think your problem is the "
public Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]");
^
it is ending your string, so you should escape it. Also you need to remove the backslash before the slash, it is no special character.
public Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,./]");
OK, once more, you wanted to match the backslash, not to escape the slash, then we end up here:
public Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,.\\\\/]");
now it is the same answer, than jdb's, so +1 to him for being quicker.
How about that?
Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,.\\\\/]");
In a character class, only [ and ] have special meaning, so you need to escape them. Plus in Java, you need to escape with an extra backslash. That's the problem specifically with Java. So, you need to use \\[ and \\]. And yes, you need to escape " with single backslash, in a string literal.
Apart from that, a hyphen when used somewhere in the middle, has also a special meaning. If you want to match a hyphen, you need to use it at the ends.
Rest of the characters, don't need to be escaped. They are just ordinary characters.
So, your pattern should be like this: -
Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,./]");
And if you want to match backslash (\) also, then use this: -
Pattern bsymbols = Pattern.compile("[-!$%^&*()_+|~=`{}\\[\\]:\";'<>?,.\\\\/]");
I am trying to split a string using a regex "A.*B", which works just fine to retrieve strings between 'A' and 'B'. But the dot '.' doesn't include new line characters \n,\r.
Can you please guide me on how to achieve this?
Thanks
Thanks all. Pattern.DOTALL worked like a charm.
I had another question related to this. What should be done if I need to extract all the strings between 'A' and 'B' (which basically match the above regex).
I tried using find() and group() of matcher class, but with the pattern below it seems to return the whole string.
Pattern p = Pattern.compile("A.*B",Pattern.DOTALL);
Use a java.util.regex.Pattern with the MULTILINE flag:
import java.util.regex.Pattern;
Pattern pattern = Pattern.compile("A.*B", Pattern.MULTILINE);
pattern.split(string);
Compile the regex with this option: Pattern regex = Pattern.compile("A.*B",Pattern.DOTALL)
Try "A[.\\s]*B"
Or you may specify the DOTALL switch so that "." will include even line terminators. Take a look aƄ the documentation of the Pattern class.
Have a look at java.util.regex.Pattern.compile(String regex, int flags), esp. the DOTALL flag
I assume you use the Pattern, Matcher classes for this.
Have you tried providing MULTILINE to your Pattern.compile() method?
Pattern.compile(regex, Pattern.MULTILINE)
'.' = Any character (may or may not match line terminators)
http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html#lt
Try changing yor regex to "A(.|\\s)*B" This means A followed by any character(.) or any white character(\s) any number of times followed by B (double scaped \s is needed at java Code).
Reference for Regular Expressions (constructs, spacial characters, etc.) in Java: http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html
I need to check a String is "\++?" which will match something like +6014456
But I get this error message invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\) .... why?
It's giving you an error because "\++?" isn't a valid Java literal - you need to escape the backslash. Try this:
Pattern pattern = Pattern.compile("\\++?");
However, I don't think that's actually the regular expression you want. Don't you actually mean something like:
Pattern pattern = Pattern.compile("\\+\\d+");
That corresponds to a regular expression of \+\d+, i.e. a plus followed by at least one digit.
I think you should use two backslashes. One for escaping the second (because it's a java string), the second for escaping the + (because it's a special character for regex).
shouldn't it be more like "\\+?" ?
Pattern pattern = Pattern.compile("\\++?");
System.out.println(pattern.matcher("+9970").find());
works for me