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 6 days ago.
Improve this question
I'm looking for a regex pattern to match any valid if statements in a java code.
So far I have if\s*\([^()]*\) which works perfectly for the if statements that doesn't have brackets in them. It is possible to have a regex to match all the if statements correctly including the ones that have brackets in them?
// Works
if (a == 2)
// works
if
(
a <= 2 || b >= 2
)
// does not work
if (a == 2 && (b == 2))
if\s*\([^{}]*\)(?:\s*\{[^{}]*\})?
This pattern matches "if" followed by optional whitespace, an opening parenthesis, any characters that are not opening or closing curly braces, a closing parenthesis, and an optional block of code enclosed in opening and closing curly braces.
this pattern assumes that there are no opening or closing curly braces inside the parentheses of the "if" statement. If that is not the case, this pattern may not work correctly.
Thanks to Rogue's comment. I've improved it to if\s*\(([^{}]*?)\)[^{};]*?[{;] which works for my needs but it still has some problems with nested if statements with no brackets and extremely complicated ones.
Related
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).
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 years ago.
Improve this question
I'm working on a regex for getting a specific number pattern from the URL string.
Requirements: Desire string should start from - or /, followed by a sequence of digits and ending with a / or nothing.
I tried: [-\/](\d+)(\/|$), but for e.g. in www.abc.com/pages/Toms-1777/14623420046 I want /14623420046(i.e. the second occurring digit sequence), but according to my regex, the result will be -1777/. I was trying negative lookbehind but not able to make any progress. I'm new to all this. Please guide.
Test cases: (with matched pattern)
www.abc.com/pages/Essen-Massage-Therapy-LLC/130561253629638
www.abc.com/biz/finn-mccools-santa-monica-2
www.abc.com/summerset.gardens.7
www.abc.com/pages/Toms-1777/14623420046
www.abc.com/pages/The-Clean-Masters/1403753595526512
www.abc.com/24hfsheepsheadbay
www.abc.com/sample2NVCoolSpace
www.abc.com/pages/Jet-Set-3920/542495615847409
www.abc.com/temp.buildings.77
www.abc.com/2423423453534temp/2312312312312312312
www.abc.com/Ptemp-Gtemp-Dtemp-189398324428792/temp
You want that $ in either case. Instead of 'slash OR end', it's more 'optional slash and then a very much not-optional end'. So.. /?$. You don't need to normally escape slashes, especially in java regexes:
Pattern.compile("[-/](\\d+)/?$")
This reads: From minus or slash, a bunch of digits, then 0 or 1 slashes, then the end. Note, use find() and not matches() - matches only works if the entire string matches, which it won't, as the - or / occurs halfway through.
EDIT: Was missing a backslash in the java string.
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.
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 need to check if a string contains number:number, and I can't find how
if(args[0] contains int:int){
code
}
Tried
if(args[0].matches("(\d{1,})[:{1}](\d{1,})")){
code
}
but it tells me
Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )
Well, the simple regex is "\\d+:\\d+".
This can be for the middle of the string or the entire string if
that is all you will allow.
I think match validates the entire string.
If you need match, and it can be in the middle, something like this might work.
"^(?s).*\\d+:\\d+.*$" and the ^$ anchors may be implicit.
if (args[0].matches(".*\\d+:\\d+.*")) {
...
OK, this is now correct. I'll leave it here in case it's useful for anyone.
note: This solution assumes args[0] will not contain newlines.
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 ,).