Regular Expression to validate a string containing '=' and '~' as end character [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 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.

Related

How to Validate a String with Colon Restriction [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
I am trying to write a boolean function to support double colons :: only for a string. It should reject any string with non-consecutive colon or more than two consecutive colons. The appearance of double colons can be any number. I can write regex which supports double colons but I don't know how to reject so many combinations of non-consecutive and consecutivec colons. Any idea is appreciated!
Valid inputs: Customer::Table, Customer::Table::Sub
Invalid inputs: Customer:Table, Customer::Table:Sub, Customer::::Table
Here is one line option using String#matches:
String input = "Customer::Table::Sub";
if (input.matches("[^:]+(?:::[^:]+)*")) {
System.out.println("MATCH");
}
Demo
Here is an explanation of the regex used:
[^:]+ match one or more non colon characters
(?: start non capturing group
::[^:]+ match :: again followed by one or more non :
)* the group occurring zero or more times

Regex to replace function foo(a,b) to function foo(b,a) [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 4 years ago.
Improve this question
I have function call like BeanUtils.copyProperties(source, destination);
I want to change it to BeanUtils.copyProperties(destination, source); in many places. How to do it using Regex? What is the regex command to do this?
I'm using eclipse to do find and replace.
Search for (with regex setting turned on)
BeanUtils\.copyProperties\s*\(\s*([\w\_]+)\s*\,\s*([\w\_]+)\s*\)\s*\;
And replace with:
BeanUtils.copyProperties($2, $1);
First escape all literal characters with backslash \
Wherever a space can be found when writing code, match it with 0 or more spaces. That by using \s* Could use [ ]* but \s might be sufficient in this case.
Then add captures for the source and destination by adding them in brackets. Or use [\w\_]+ to match other variable names. With a + to mean at least 1 char. NB: if your variable have any other non-alphanumeric chars, add them to the [...] list.
Finally in the replace, switch the captures.

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)

Explain this regular expression in Java [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 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+", ""));

How this regular expression do this? [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 8 years ago.
Improve this question
I want to use below expression in my program but i don't know what do this regular expression!
please help me.
"(?=(?!^)[,;.:])|(?<=[,;.:])"
in the above expression (?=(?!^)[,;.:]) find any character set that end with [.;,:] or no? what do this (?!^) in this expression?
and this expression find any character set that end with [,;.:] or no?
please help me.
The expression matches 0-length strings that satisfy one of these two conditions:
Ahead of it is one of ,;.:, but not for 0-length strings just before the beginning of the subject string (position 0). So the subject string "." has no match at position 0, only at position 1 because of the following alternative. This is done with positive lookahead (?=) and negative lookahead (?!).
Behind it is one of ,;.:. This is done with positive lookbehind (?<=).
For instance for "aaa,1", you have two matches: at position three (after the last a, because it's followed by ,) and at position 4 (because it's preceded by ,).

Categories