Regex java need some tips [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 7 years ago.
Improve this question
So I have to use Regex for the name of a character, where the name has to start with a capital and can only exist out of letters, apostrophe's and spaces.
And I dont know how to start, could anyone help me with this?

I suppose your character name is stored in a String variable. For that you can use the String.matches() function, which accepts a regex String parameter.
To create the required regex you will have to combine the folowing:
[A-Z] for the capital letter
[a-z' ]+ for the remaining characters.
Note that when using those in Java you'll need to add some escape characters
You can experiment with regular expressions here: http://www.regexr.com

Related

Regular Expression to match first character and after a character [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 the following string:
___abcd-metadata.json
I am trying to do a regex to get ___ and everything after -. So far I have this Regex:
(\-.*?\.json)
To find the last part and (___) to find the first part, but I have not been able to figure how to combine both regexes to make this happen.
My desired result would be ___ -metadata.json = true
"(\-)(.*)" is the pattern, which will give you 2 matches if the string contains them.
Take your string and use String.split('-') to break it into parts.
EDIT
var parts = ('_abcd-metadata.json').split('-'),
start = parts[0].substr(0,3),
rest = parts[1];

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.

Simple Regular expression Java, brackets and doubles [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 6 years ago.
Improve this question
This is very basic but can't seem to find a answer when searching.
I have a string that's in the format:
[[[0.093493,51.6037],[0.091015,51.5956],[0.088596,51.5857]]]
The doubles inside the brackets are [latitude coordinate,longitude coordinate].
From this I'd like to extract the coordinates.
What should I put inside the search pattern if I use a Pattern/Matcher solution?
Assume that format with brackets is always correct but the doubles can vary in length.
Basicly what I want the code to do is:
Find "[" left of a number, then find this "," and return what's in between
AND another searchpattern that:
find "," and "]" and return what's inbetween.
Keep it simple by using this regex:
\[(\d+\.\d+),(\d+\.\d+)\]
and repeat the matcher.find() till all matches are found.
You matches are in group #1 and group #2
RegEx Demo

Is there a way to use \p{Punct}\p{Lower}\p{Upper} and in a regex(java), but without the "." character? [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
I need to take out all the characters of a string that are not numbers.
You could use a Character Class Intersection like [\p{Punct}\p{Lower}\p{Upper}&&[^.]]
But why not just use
[^\d.]+
As Java String "[^\\d.]+"
This would match one or more characters, that are not \d a digit or the . period.
I'd suggest using \\d+ then (it's consecutive digits), and a capture group. Something like
String str = "";
str = str.replaceAll("(\\d+\\.\\d+)", "$1");

What would be regular expression of finding all strings starts with $ in java [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 9 years ago.
Improve this question
I am having one string containing "This is a time to get involve into $FO, $RTP, $DFG and $RG"
Use following regular expression:
"\\$\\w+"
$ should be escaped.
\w match digits, alphabet, _.
If you need only match alphabets, use [a-zA-Z] instead.
This will work too
String str="This is a time to get involve into $FO, $RTP, $DFG and $RG" ;
String[] arr=str.split(" ");
for (String i:arr){
if(i.indexOf("$")==0){
System.out.println(i.replaceAll("\\,",""));
}
}

Categories