Explain this regular expression in Java [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Regular expression in java:
'String'.replaceAll("([aeioucgjkqsxyzbfpvwdtmn1234567890])\\1+", "$1")
Can someone explain what the different characters do?

Explanation:
[aeioucgjkqsxyzbfpvwdtmn1234567890] Matches a single character in the list.
([aeioucgjkqsxyzbfpvwdtmn1234567890]) Capturing group around the char class would capture that single character.
\1+ \1 is a pointer to refer the chars inside the group index 1. In our case, a single character is captured so it refers to that single character. \1+ means one or more occurrences of the characters inside group index 1.
For Example:
aaaa
The above regex would capture the first character and check if the following one or more characters are same as the first character which was captured. If yes, then the whole duplicated chars are replaced by a single char(which was inside group index 1 ), that is aaaa was replaced by a single a
DEMO

All letters that are listed between brackets will be replaced by $1 if after them comes a \1, which is a literal backslash one. The plus sign (+) means 1 or more.

Any sequence of 1 or more of the characters inside the brackets [...] will be replaced with $1.
For instance, this will remove all those characters from your string:
System.out.println(Str.replaceAll("([aeioucgjkqsxyzbfpvwdtmn1234567890])\1+", ""));

Related

Check if word alternates consonant and vowel [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
So I need to check if a word is a pattern of alternating vowel and cosonant (Or consonant and vowel) in Java.
I want to make it a regex but I just came with this incomplete regex expression:
[aeiouAEIOI][^aeiouAEIOI]
Any ideas?
Thanks :)
Update: It's not regex restricted, so it can be an option if anyone has any ideas
One way is using a lookahead to check if neither two vowels nor two consonants next to each other.
(?i)^(?!.*?(?:[aeiou]{2}|[^aeiou]{2}))[a-z]+$
See this demo at regex101 (used i flag for caseless matching, the \n in demo is for staying in line)
Update: Thank you for the comment #Thefourthbird. For matching at least two characters you will need to change the last quantifier: Use [a-z]{2,} (two or more) instead of [a-z]+ (one or more). For only matching an even amount of characters (2,4,6,8...), change this part to: (?:[a-z]{2})+
FYI: If you use this with matches you can drop the ^ start and $ end anchor (see this Java demo).

Regular Expression to validate a string containing '=' and '~' as end character [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
In Java, I have to validate a string which contains "~" and '=' at the end using RegEx.
For example:
LOCKER=2004-02-23-23.28.22.377655~UCC=0103207031~URY=31/12/9999~URF=23/02/2004~URT=SEREST ISSY LES MO ~URFC=XX~URFNUMCB=XXXXXXXXXXX~CEB=XXXXX~CEBC=XXXXX~URFN=0001
this String format is KEY1=VALUE~KEY2=VALUE~KEYN=VALUE uppercase
'~' is as separator
Currently, i am using some regular expresion but all of them false
can anyone help me please ? thank you for advanced
The following regex should do:
^(?!~)(?:(?:^|~)[^=~]+=[^=~]*)+$
Explanation
^ Match beginning-of-input, i.e. matching must start at beginning
(?!~) Input cannot start with `~`
(?: Repeat 1 or more times:
(?:^|~) Match beginning of input or match '~', i.e. match nothing on
first repetition, and match `~` on each subsequent repetition
[^=~]+ Match KEY
= Match '='
[^=~]* Match VALUE (may be blank)
)+
$ Match end-of-input, i.e. matching must cover all input
Change the characters classes for KEY and VALUE as needed if they have further restrictions, e.g. use [A-Z][A-Z0-9]* instead of [^=~]+ if the KEY has to be an uppercase-only identifier.
If using Java's matches() with the regex, the first ^ and the ending $ is redundant. The second ^ is still required.

Regular expression that matches only up to 10 decimal places [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
so I'm trying to write a regular expression that can match the following scenarios.
12.1234 = match
112.12345678901 = wont match
1287729918192.123 = match
123927678281818.19883748383839 = won't match
23.1829aga = won't match
1722ahh.98822 = won't match
1.#$122 = won't match
Basically it should only match strings that contain no letters or special characters and values with less than 10 decimal places.
I'm really new to regular expression and am not sure how to accomplish this.
Thank you for any help!
Trick to writing simple regexes:
describe your pattern in words
look for quantifiers, character classes and/or other tokens that satisfy your description
combine all the tokens you found into one line!
Basically, your pattern can be described like this:
start of the string
1 to unlimited number of digits
a dot
between 1 and 10 digits
end of the string
We just translate the above descriptions into regex:
^
\d+
\.
\d{1,10}
$
And combine all these:
^\d+\.\d{1,10}$
This should work:
^\d*\.\d{1,10}\s
To deconstruct this a little bit:
"^" means starting at the start of the line of text
"\d*" means any number of digit characters (including zero!)
"." means the literal '.' character (we need to escape it with a '\' first)
"\d{1,10}" means between 1 and 10 amount of digits
"\s" means any type of whitespace
I would recommend you take a look at https://regexone.com/. An excellent resource that will help you understand how regex works, it helped me a lot!
Try this:
/^[0-9]*[.]{0,1}[0-9]{1,10}$/
The Explanation follows:
^ asserts position at start of the string
[0-9]* Match a single character in the range between 0 (index 48) and 9 (index 57) any times
[.]{0,1} Match a single character . at max one times
[0-9]{1,10} Matches between 1 and 10 times, a single character in the range between 0 (index 48) and 9 (index 57)

Escaping space and equal to character [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am using java and have string which could have multiple spaces and equal to "=" sign as shown below.
String temp = "[name='FPC:CPU']/XM chip/allocate";
This temp string will passed to some other program which is failing because of space and equal sign.
How can i escape space and "=" character?
My desired out put from original string
[name='FPC:CPU']/XM chip/allocate
to
[name\='FPC:CPU']/XM\ chip/allocate
Wondering how can i do that using temp.replaceAll
That should be pretty straight forward.
System.out.println("foo bar=baz".replaceAll("([ =])" "\\\\$1"));
Should print this
foo\ bar\=baz
The parenthesis in the regular expression form a capturing group, and the character class [ =] will capture spaces and equal signs.
In the replace expression, the $1 refers to the first capturing group. The only thing that gets a bit tricky is escaping the backslash.
Normally, in a regular expression replacement the backslash itself is an escape character. So you'd need two of them together to insert a backslash, however backslash is also an escape in a Java String, so to put two backslashes into a Java String (to form the regular expression escape), you must insert four backslashes. So that's how you end up with "\\$1".

Blank space in array with String splitter [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
My objective is to separate the numbers from the string, but in my array's first position i get a blank space. So i need help for that not to happen.
str1 = "Y=9x1+29x2";
String[] split2 = str2.split("[^-?.?0-9]+");
Blank space at the start is due to presence of non-digit character at the start of your input.
You can remove all non-digits at start before splitting:
String linha = "Y=9x1+29x2";
String[] split = linha.replaceFirst("[^-.\\d]+", "").split("[^-.\\d]+");
for (String tok: split2)
System.out.println(tok);
Output:
9
1
29
2
I think your question is rather vague, but after looking at it, I'm guessing that you want to extract the numbers out of the string, where a "number" has this format: an optional minus sign, followed by an optional decimal point, followed by one or more digits. I suspect you also want to include numbers that have digits followed by a decimal point followed by more digits.
I'm guessing this is what you want, because of the ? you put in your regex. The problem is that inside square brackets, ? doesn't mean "optional", and it doesn't mean "zero or one of something". It means a question mark. The regex [^-?.?0-9] means "match one character that is not a digit, a period, a hyphen, or a question mark". A pattern in square brackets always matches one character, and you tell it what characters are OK (or, if you begin with ^, what characters are not OK). This kind of "character set" pattern never matches a sequence of characters. It just looks at one character at a time. If you put + after the pattern, it still looks at one character at a time; it just does so repeatedly.
I think what you're trying to do is to take a pattern that represents a number, and then say "look for something that doesn't look like that pattern", and you tried to do it by using [^...]. That simply will not work.
In fact, split() is the wrong tool for this job. The purpose of split is to break up a string whose delimiters match a given pattern. Using it when the strings you want to keep in the array match a given pattern doesn't work very well, unless the pattern is extremely simple. I recommend that you create a Matcher and use the find() method in a loop. find() is set up so that it can find all matching substrings of a string if you call it repeatedly. This is what you want to accomplish, so it's the right tool.

Categories