Regex find digits after String [duplicate] - java

This question already has answers here:
Difference between matches() and find() in Java Regex
(5 answers)
Closed 6 years ago.
From the two urls
https://twitter.com/realDonaldTrump/status/829684271812067328
https://twitter.com/realDonaldTrump/status/829684271812067328/
I want to extract the String 829684271812067328 in Java.
My attempt was
\\/status\\/([\\d]+)
but this does not allow anything before /status or after the digits (/).
Whats the solution to this?

If you just need to extract data (rather than validate the url format), I'd use the following regex :
(\\d+)/?$
And extract the first group of the result.
It matches a non-empty sequence of digits that can be followed by a / and must be found at the end of the matched text.

Related

How to replace the end of a string? [duplicate]

This question already has answers here:
Replacing last character in a String with java
(13 answers)
Closed 3 years ago.
I got a string (URL) which ends with "hl=en", "hl=ru", "hl=it" etc with other languages. I want to replace (always) last 5 symbols to "hl=en". What kind of regex should I use?
Well if you really want to use Regex you could use:
str.replaceAll(".{5}$", "hl=en");
Will get 5 characters followed by a 'end of line/string' character and replace it with the desired string

Regex pattern throwing PatternSyntaxException in Java [duplicate]

This question already has answers here:
Allow - (dash) in regular expression
(3 answers)
Closed 3 years ago.
I have a regex pattern which checks for client auth domain name in certificate matching the pattern.
However it is throwing patternsyntax exception.
Pattern am using is below:
^(?!\s)([a-zA-Z0-9.-\s]{1,128})$
Exception is invalid character range near index 21. I suppose it is for -/s in the range. Is there a way to change the regex pattern? Can I use -/s at start of character range? Help would be appreciated.
You just need to escape the - symbol if you are trying to match it. So the correct regex would look as follow:
^(?!\s)([a-zA-Z0-9.\-\s]{1,128})$
I suggest you to use one of many online available Regex tools when you want to learn and build your regex.

RegEx ignore letters until digit occurs and then match letters afterwards [duplicate]

This question already has answers here:
Using Regular Expressions to Extract a Value in Java
(13 answers)
Closed 3 years ago.
This is intended to be used in Java.
Imagine following sample input:
WRA1007
1085808
1092650S
3901823CV
I want to match all alphabetic characters after at least one digit.
Desired output:
S
CV
Actual output:
0S
3CV
My current approach looks like this:
\d[a-zA-Z]+
The problem with this pattern is that it includes the digit beforehand too. My current solution is to remove the first character of the resulting string afterwards. And this seems quite unsatisfactory to me.
You need a lookbehind:
(?<=\d)[a-zA-Z]+
(?<=\d) means "there must be a digit before this position, but don't match it".
Demo
Alternatively, you can use a pair of () to surround the part you want to get:
\d([a-zA-Z]+)
This is called a "group", and you can get its value by calling group(1) on your Matcher.
If you 'add' groups you can get group 1 that contain only letters
\d([a-zA-Z]+)

Java regex for consecutive exponential terms [duplicate]

This question already has answers here:
My regex is matching too much. How do I make it stop? [duplicate]
(5 answers)
Regex to first occurrence only? [duplicate]
(4 answers)
Closed 4 years ago.
I've tested this regex:
String regex = "[e][#](.)+[$]
the regex works well to identify exponential terms, however it breaks when there are two or more consecutive exponential terms.
I test the regex with the usual code:
while(matcher.find()){
String string = matcher.group();
System.out.println("this one: "+string);
}
When I type the expression:
e#x$ + 3e#x+1$
string equals to (e#(x$+3e#x+1$))
By the way, I added the parentheses inside the while loop. They are necessary for
what I am trying to accomplish.
I want the result of string to be (e(#x$))+3(e(#x+1$)
I know the problem lies in "(.)+ What I think is happening is that the regex
includes the first $, what I need is for it to stop at the first $.
How can I include this logic inside the regex?
thank you

This pattern matches for input 123456789.2.2.2 , which it should not [duplicate]

This question already has answers here:
Java RegEx meta character (.) and ordinary dot?
(9 answers)
Closed 6 years ago.
I am trying to solve following task:
Match the pattern abc.def.ghi.jkl, where each variable a,b,c,d,e,f,g,h,i,j,k,l can be any single character except the newline.
For above question I am matching the input to regex :
"([^\\n]{3}(.)){3}([^\\n]{3})"
// this is the regex pattern I am using currently
What am I doing wrong? Please help me correct the above regex so that it does not match the incorrect input I have provided in the title. Currently it matches to it somehow. Although I have provided 3 it is apparently matching to more than 3 characters.
. has a special meaning in regular expression patterns.
If you want to get a "simple dot", you need to quote/escape it (as "\\.").
And that special meaning is (under normal configuration) "any character except line breaks", which exactly matches your other condition, so you can simplify this to
"(...)\\.(...)\\.(...)\\.(...)"

Categories

Resources