I am dealing with a String,
30-Nov-2012 30-Nov-2012 United Kingdom, 31-Oct-2012 31-Oct-2012 United Arab Emirates, 29-Oct-2012 31-Oct-2012 India
I need to add spaces every time a space appears after a four digit numbers, i.e.:
30-Nov-2012#30-Nov-2012#United Kingdom, 31-Oct-2012#31-Oct-2012#United Arab Emirates, 29-Oct-2012#31-Oct-2012#India
I don't know how to write Regular Expressions, any help please?
Since you don't know how to write Regular Expressions, your best bet is learning how to use Regular Expressions.
Here's a pretty good tutorial.
http://www.vogella.com/articles/JavaRegularExpressions/article.html
The expression you want to write doesn't seem very complicated. You should be able to do it after going through this tutorial.
Edit: Here's another hint. Take a look at replaceAll
and also take a look at positive lookbehinds
Edit2: I'm bored, so here's the answer.
string.replaceAll("(?<=\\d{4})\\s", "#");
Try this regex:
(\d{4})\s+
replace with
$1#
and a sample code:
String result = inputString.replaceAll("(\\d{4})\\s+", "$1#");
explain:
\d
Matches any decimal digit.
{n}
Matches the previous element exactly n times.
\s
Matches any white-space character.
+
Matches the previous element one or more times.
(subexpression)
Captures the matched subexpression and assigns it a zero-based ordinal number.
$ number
Substitutes the substring matched by group number.
Related
I am trying to use the following regex to capture following values. This is for use in Java.
(\$|£|$|£)([ 0-9.]+)
Example values which I do want to be captured via above regex which works.
$100
$100.5
$100
$100.6
£200
£200.6
But the following as gets captured which is wrong. I only want to capture values when thereis only 1 dot in the text. Not multiples.
£200.15.
£200.6.6.6.6
Is there a way to select such that multiple periods doesn't count?
I can't do something like following cos that would affect the numbers too. Please advice.
(\$|£|$|£)([ 0-9.]{1})
You can use
(\$|£|$|£)(\d+(?:\.\d+)?)\b(?!\.)
See the regex demo.
In this regex, (\d+(?:\.\d+)?)\b(?!\.) matches
(\d+(?:\.\d+)?) - Group 1: one or more digits, then an optional occurrence of . and one or more digits
\b - a word boundary
(?!\.) - not immediately followed with a . char.
Another solution for Java (where the regex engine supports possessive quantifiers) will be
(\$|£|$|£)(\d++(?:\.\d+)?+)(?!\.)
See this regex demo. \d++ and (?:\.\d+)?+ contain ++ and ?+ possessive quantifiers that prevent backtracking into the quantified subpatterns.
In Java, do not forget to double the backslashes in the string literals:
String regex = "(\\$|£|$|£)(\\d++(?:\\.\\d+)?+)(?!\\.)";
You could try this
(\$|£|$|£)([0-9]+(?:\.[0-9]+)?)$
one or more digits followed by an optional dot and some digits and then the end of the string.
EDIT: some typos fixed
And it's not ok to delete the whole sentence obove, due to one word against my self. :(
I need to match text similar to the following text in an if statement.
REG#John Smith#14102245862#7 johns road new york#John Anthony Smith
The expression is meant to match a REG keyword at the beginning of the string then username followed by an account number composed of numbers with no specific restriction on the number of digits, then the address and lastly the name of the individual the address is registered to.
The Regex expression I had come up with is not working. The regex expression is below:
^REG\#\w\#[0-9]\#\w\#\w
May you kindly assist in showing me where I went wrong and how to make it work.
Thank you in advance
The problem is that you don't use quantifiers (* or +) and space is not included within \w which stands for [A-Za-z0-9_]. The character # does not need to be escaped (at least as far as I know in Java). Try the following Regex:
^REG#[\w ]+#\d+#[\w ]+#[\w ]+
^REG matches the beginning of the string (REG) literally
# matches self literally
[\w ]+ stands for at least one word character or space
\d+ stands for at least one digit
In Java, don't forget the double escaping:
String regex = "^REG#[\\w ]+#\\d+#[\\w ]+#[\\w ]+";
Try ^REG\#.*?\#[0-9]*?\#.*?\#.* , the operator *? means repeat until next slice of expression, in that case, \#
Example
The no.s 1234 65
Input: n
For n=4, the output should be 1234
For n=2, the output should be : 65 (not 12)
Tried \d{n} which gives 12 and \d{n,} gives 1234 but i want the exact matching one.
Pattern p = Pattern.compile("//\d{n,}");
you need negative lookaround assertion: (?<!..): negative look behind, and (?!..): negative look ahead : regex101
(?<!\d)\d{4}(?!\d)
however not all regex engine supports them, maybe a work around may match also the preceeding character and following character (contrary to look-around which are 0 width matches), (\D matches all excpet a digit)
(?:^|\D)(\d{4})(?:\D|$)
I think what you meant is the \b character.
Hence, the regex you're looking for would be (for n=2):
\b\d{2}\b
From what I understand, you're looking for a regex that will match a number in a string which has n digits, taking into into account the spacing between the numbers. If that's the case, you're looking for something like this:
\b\d{4}\b
The \b will ensure the match is constrained to the start/end of a 'word' where a word is the boundary between anything matched by \w (which includes digits) and anything matched by the opposite, \W (which includes spaces).
I don't code in java but I can try to answer this using regex in general.
If your number is in the format d1d2d3d4 d5d6 and you want to extract digits d5d6, create 3 groups as r'([0-9]+)("/s")([0-9]+)' – each set of parenthesis () represent one group. Now, extract the third group only in another object which is your required output.
if(password.matches("(?=.*[0-9].*[0-9])(\\w{8,})") )
System.out.println("Valid Password");
else
System.out.println("Invalid Password");
I am checking a password to ensure it has at least 8 characters in length, which can be letters or digits and it must have at least 2 digits. This appears to work for me but I just wanted to confirm I was doing this right. Also, I have been trying to research and figure out exactly what each piece is doing. Below is what I believe each piece to be doing, but if I am incorrect, would you please explain what the particular portion is actually doing. Thanks
?= tells the program to remember if the digits [0-9] which I am looking for are found ?
.* says for any number of [0-9]?
[0-9] Specifies any number from 0-9.
.*[0-9] Then the regex looks for another number from 0-9 ?
(\\w{8,}) looks for any letters (uppercase or lowercase) and digits, with a minimum length of 8 characters?
That regex has two main parts:
(?=.*[0-9].*[0-9])
(\\w{8,})
Part 1. is a positive look ahead, which has the form (?=pattern). "Look arounds" (positive/negative look behinds/aheads) assert, without consuming (or capturing), that the adjacent input matches a certain pattern. In this case, it's asserting that the input following the current point contains (at least) 2 digits (.* meaning 0-n chars, [0-9] meaning any number character). Incidentally, it could be expressed more succinctly as (?=(.*[0-9]){2}
Part 2. means "at least 8 word characters" - a word character is any letter, any number or an underscore. The brackets around it (unnecessarily) capture the 8+ word characters as group 1
?= is a positive look ahead, That means that it is searching for something ahead of it.
http://www.regular-expressions.info/lookaround.html
For more info on the look ahead.
http://rubular.com/
Great for testing out any regex.
^[0-9]\\d*(\\.\\d+)?$
I can't quite work out what the above regex pattern is looking for. I'm tempted to interpret it as "find anything that is not the numbers 0-9 inclusive, then find zero or more occurrences of a single digit, then find zero or one occurrences of a decimal point followed by at least one digit" but I'm not sure.
Part of my confusion stems from the fact that in the SCJP6 certification book, the not operator is included inside the square brackets, whereas here it's outside. Also, I am just generally inexperience when it comes to regex.
Can someone please help? [This is from a Java program. Is the above in any way Java specific?] Thanks.
^ start of a string
[0-9] a single digit
\\d* any amount of digits (0-infinity)
(\\.\\d+)? Once, or not at all: a dot followed by at least one digit
$ end of string.
You have a complicated regex that will match any floating point or non floiting point number.
Have a look at the java.util.Pattern class and and the Oracle Java Regex Tutorial.
It is looking a one or more digits, optionally followed by a . and one or more digits. It is confusing as it is needlessly complicated. It is the same as
^\\d+(\\.\\d+)?$
\d is defined as A digit: [0-9]
When the "^" operator is outside of a character class "[]" it denotes the start of input, "$" defines end of input.
So your description is correct, but it should be changed to:
find a single digit from zero to nine...
for more information about regular expressions check out this link