Java check if a string contains int:int [closed] - java

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.

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.

Splitting a comma-separated string but ignoring commas in whole word [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 have a string like:
String test = "firstName:a,lastName:b,addressOne:line 1,line 2,city:other";
i am trying to do test.replaceAll(",","\",\"").I mean replace , with ",". I want to this only for the whole strings. For eg addressOne is a single string with comma seperated i dont want to replace that. Is there any regex or someother way i can do this?
i should get a string like
"firstName":"a","lastName":"b","addressOne":"line 1 , line 2","city":"other"
after replace , but i am getting
"firstName":"a","lastName":"b","addressOne":"line 1 "," line 2","city":"other".
You can split your string with commas that are followed by a word which has been followed by :. And for this aim you can use a positive look-ahead and for matching the leading word use a negated character class [^:,]+ which will match any string except : and ,:
test.split(",(?=[^:,]+:)")

Regex java need some tips [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 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

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