Regex for extraction numbers? [duplicate] - java

This question already has answers here:
How to extract numbers from a string and get an array of ints?
(13 answers)
Closed 8 years ago.
As i'm really bad in REGEX, I'm looking for some help here. a string can be of the following two formats:
1. id://3
2. id://3/1
How can i check if one of the two formats is available (because there's e.g. id://next – id + strings – as well) and how can i extract in the first case the "3" and the second case the "3" and "1" in separate variables? the numbers can be anything (3,1 is just an example).
thanks in advance!

You can use this regex:
\bid:\/.*?\/(\d+)\b(?!\/)
RegEx Demo

Related

How do I make sure the length is 6 in this regular expression? [duplicate]

This question already has answers here:
Using explicitly numbered repetition instead of question mark, star and plus
(4 answers)
Closed 2 years ago.
I have this regular expression that I wrote, which extracts text in between tags like "#<string to extract>":
"#<(.+?)>"
I need to make sure that the length of the string I'm extracting is 6 and my current solution is checking the length of the string that I extracted with an if statement. I would like to replace this with a regex instead. How could I modify "#<(.+?)>" to make sure it is 6 characters when extracted?
You can use curly braces to specify the length of the match.
#<(.{6})>

How to remove all but certain characters from a string in Java? [duplicate]

This question already has answers here:
How to filter string for unwanted characters using regex?
(7 answers)
Closed 5 years ago.
Assume you had a string "ACcwerwervwvrwBq^2424 /.* DffGZ..'B". How would you only keep certain characters like A,B,C,D and remove the rest?
string.replaceAll seems to work if I know what characters to remove, but I want to remove all characters except A,B,C,D. Putting every character in there except those 4 seems pretty tedious, what the easier way?
I want the output in the above case to be "ACBDB".
You would use regex something like:
str.replaceAll("[^ABCD]", "");
Should do it
You just need a proper regex:
s.replaceAll("[^ABCD]", "")

How to use Split in report's expression [duplicate]

This question already has answers here:
How do I split a string in Java?
(39 answers)
Closed 4 years ago.
Currently attempting to split a large string field into 3 smaller fields. The string delimited by a "/". Example String:
0123/ABCD1234/EFGH909883432212
At the moment I have managed to pull the middle section out using the following expression inside a variable:
$F{String}.split("/" ,5)[1].trim()
To be perfectly honest I am not sure how it works as I do not know what the 5 and 1 are for (which is probably what I need to know to get the other two sections)
After calling method spit an array is created holding substrings which are delimited by "/" in the original string. The trim removes any trailing spaces.
Number 5 resembles optional parameter.
An integer that specifies the number of splits, items after the split limit will not be included in the array.

StringUtils isNumeric returns true when input is "???", why? [duplicate]

This question already has answers here:
Why does Apache Commons consider '१२३' numeric?
(5 answers)
Closed 6 years ago.
I was reading the commons.apache.org isNumeric method definition and it states:
StringUtils.isNumeric("???") = true;
I am not sure why "???" is considered to be numeric. My guesses are:
A "?" is considered a unicode digit
It is some kind of regex pattern
I was able to find the answer to this question by looking at the StringUtils source code for the isNumeric method.
In the source code that line appears as:
StringUtils.isNumeric("\u0967\u0968\u0969") = true
Where u0967, u0968, u0969 are Devangari Digits one, two, and three respectively.
This may be a browser issue causing the characters to not be rendered correctly in the API.
Looking at the code, the example is
StringUtils.isNumeric("\u0967\u0968\u0969") = true
\u0967 is १, which is "Devanagari Digit One"
\u0967 is २, which is "Devanagari Digit Two"
So they are digits!

How do you read a certain number of characters in a file? [duplicate]

This question already has answers here:
Reading a plain text file in Java
(31 answers)
Closed 6 years ago.
For example, there's an extremely long chain of characters in a txt file and you wish to iterate through it to find a word or integer combination, analogous to a word search.
I won't post an example because you didn't even try. However you should research regular expressions.
Java regular expression match
http://www.tutorialspoint.com/java/java_regular_expressions.htm

Categories