Regex pattern needed for 123.12/23 - java

I am trying to check my string having 123.12/23 with pattern \\d+(.\\d+)*\\/\\d+(.\\d+)* but it is not working, it is passing 123.12/23/24 also.
I need below scenarios to be covered :
Strings to be passed : 12/23 , 12.23/23 , 12/23.33
Strings to be failed : 12/13/14 , 12.23/2/4

^\d+(?:\.\d+)?\/\d+(?:\.\d+)?$
You were close.Escape the ..See demo.
https://regex101.com/r/iJ7bT6/1
For java it would be
^\\d+(?:\\.\\d+)?\\/\\d+(?:\\.\\d+)?$

Related

Java recursive/repeated regex

I am trying to replace all .(periods) with keyword XXX which lie within an alphanumeric word in a large text.
For example: I am trying to match a.b.c.d.e ...
Expected output: I am trying to match aXXXbXXXcXXXdXXXe ...
Pattern I used: (\w+)([\.]+)(\w+)
Actual result: I am trying to match aXXXb.cXXXd.e ...
How can I get expected output via regex without using any code/stubs.
You can use lookarounds:
str = str.replaceAll("(?<=[a-zA-Z0-9])\\.(?=[a-zA-Z0-9])", "XXX");
RegEx Demo
Lookaround Reference
Why don't you do something like if you want to change all . -
str = str.replaceAll("\\.", "XXX");
Or below if you don't want to change . if any first or last index -
str = str.replaceAll("\\.", "XXX").replaceAll("^XXX", ".").replaceAll("XXX$", ".");

Java(Apex) RegEx not working?

I am having trouble with a regex in salesforce, apex. As I saw that apex is using the same syntax and logic as apex, I aimed this at java developers also.
I debugged the String and it is correct. street equals 'str 3 B'.
When using http://www.regexr.com/, the regex works('\d \w$').
The code:
Matcher hasString = Pattern.compile('\\d \\w$').matcher(street);
if(hasString.matches())
My problem is, that hasString.matches() resolves to false. Can anyone tell me if I did something somewhere wrong? I tried to use it without the $, with difference casing, etc. and I just can't get it to work.
Thanks in advance!
You need to use find instead of matches for partial input match as matches attempts to match complete input text.
Matcher hasString = Pattern.compile("\\d \\w$").matcher(street);
if(hasString.find()) {
// matched
System.out.println("Start position: " + hasString.start());
}

Java replace pattern

I need to replace the ROOMS start and end tag from an xml file.
<A><ROOMS><B></B></ROOMS></A>
becomes
<A><B></B></A>
And also
<A><ROOMS><B></B></ROOMS></A>
becomes
<A><B></B></A>
I tried
Pattern.compile("\\\\\\\\<(.*)ROOMS\\\\\\\\>").matcher(xml).replaceAll("")
, but it does not work.
Can anybody help me?
Your regex is absurd. Just use:
xml = xml.replaceAll( "</?ROOMS>", "" );
Try using
<[/]?ROOMS>
as your pattern. It uses the ? flag to indicate that the XML-closing forward slash should occur 0 or 1 times.
You can probably use this regex :
<[\/]?ROOMS>

Replace design pattern in query string

I have currently some URL like this :
?param=value&offset=19&size=100
Or like that :
?offset=45&size=50&param=lol
And I would like to remove for each case the "offset" and the "value". I'm using the regex method but I don't understand how it's really working... Can you please help me for that?
I also want to get both values of the offset and the size.
Here is my work...
\(?|&)offset=([0-9])[*]&size=([0-9])[*]\
But it doesn't works at all!
Thanks.
Assuming is Javascript & you only want to remove offset param:
str.replace(\offset=[0-9]*&?\,"")
For Java:
str=str.replaceAll("offset=[0-9]*&?","");
//to remove & and ? at the end in some cases
if (str.endsWith("?") || str.endsWith("&"))
str=str.substring(0,str.length()-1);
With out regex .
String queryString ="param=value&offset=19&size=100";
String[] splitters = queryString.split("&");
for (String str : splitters) {
if (str.startsWith("offset")) {
String offset = str.substring(str.indexOf('=') + 1);//like wise size also
System.out.println(offset); //prints 19
}
}
If you need to use a regular expression for this then try this string in java for the regular expression (replace with nothing):
"(?>(?<=\\?)|&)(?>value|offset)=.*?(?>(?=&)|$)"
It will remove any parameter in your URL that has the name 'offset' or 'value'. It will also conserve any required parameter tokens for other parameters in the URL.

REGEX to get flags from console command?

I'm trying to get a regex that can pull out the flags and values in string. Basically, I need to be able to take a string like this:
command -aparam -b"Another \"quoted\" param" -canother one here
And capture the data:
a param
b Another "quoted" param
c another one here
Here is my Java regex so far:
(?<= -\w)(?:(?=")(?:(?:")([^"\\]*(?:\\.[^"\\]*)*)(?="))|.*?( -\w|$))?
But is doesn't quite work yet. Any suggestions?
The suggestion is to use one of available CLI parsers. For example CLI from Jakarta or, better, args4j.
Tokenize the string into command and its parameters using split method,
String input = "command -aparam -b\"Another \"quoted\" param\" -canother one here ";
String[] cmds = input.split("\\s*-(?=\\w)");

Categories

Resources