i want to remove anything between "?" and "/"
my text is "hi?0/hello/hi"
i need to see this out put
"hi?/hello/hi"
My Code Is
key.replaceAll("\\?.*/","?/");
but my Output Is
"hi?/hi"
whats wrong?
You are using greedy matching, so it matches up to the next slash too. Try:
key.replaceAll("\\?.*?/","?/");
An alternative still using greedy matching is to match any character except /:
key.replaceAll("\\?[^/]*/","?/");
Use this:
key.replaceAll("\\?.*?/","?/")
You can read more about greedyand non greedy matching here
Related
I am writing a regex that will be used for recognizing commands in a string. I have three possible words the commands could start with and they always end with a semi-colon.
I believe the regex pattern should look something like this:
(command1|command2|command3).+;
The problem, I have found, is that since . matches any character and + tells it to match one or more, it skips right over the first instance of a semi-colon and continues going.
Is there a way to get it to stop at the first instance of a semi-colon it comes across? Is there something other than . that I should be using instead?
The issue you are facing with this: (command1|command2|command3).+; is that the + is greedy, meaning that it will match everything till the last value.
To fix this, you will need to make it non-greedy, and to do that you need to add the ? operator, like so: (command1|command2|command3).+?;
Just as an FYI, the same applies for the * operator. Adding a ? will make it non greedy.
Tell it to find only non-semicolons.
[^;]+
What you are looking for is a non-greedy match.
.+?
The "?" after your greedy + quantifier will make it match as less as possible, instead of as much as possible, which it does by default.
Your regex would be
'(command1|command2|command3).+?;'
See Python RE documentation
I am totally confused right now.
What is a word that matches: ^.*(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$
I tried at Regex 101 this 1Test#!. However that does not work.
I really appreciate your input!
What happens is that your regex seems to be in Java-flavor (Note the \\d)
that is why you have to convert it to work with regex101 which does not work with jave (only works with php, phyton, javascript)
see converted regex:
^.*(?=.*\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$
which will match your string 1Test#!. Demo here: http://regex101.com/r/gE3iQ9
You just want something that matches that regex?
Here:
a1a!
This pattern matches
\dTest#!
if u want a pattern which matches 1Test#! try this pattern
^.(?=.\d)(?=.[a-zA-Z])(?=.[!##$%^&]).*$
Your java string ^.*(?=.*\\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$ encodes the regexp expression ^.*(?=.*\d)(?=.*[a-zA-Z])(?=.*[!##$%^&]).*$.
This is because the \ is an escape sequence.
The latter matches the string you specified.
If your original string was a regexp, rather than a java string, it would match strings such as \dTest#!
Also you should consider removing the first .*, doing so would make the regexp more efficient. The reason is that regexp's by default are greedy. So it will start by matching the whole string to the initial .*, the lookahead will then fail. The regexp will backtrack, matchine the first .* to all but the last character, and will fail all but one of the loohaheads. This will proceed until it hits a point where the different lookaheads succeed. Dropping the first .*, putting the lookahead immidiately after the start of string anchor, will avoid this problem, and in this case the set of strings matched will be the same.
I have searched everywhere and I cannot find what I am doing wrong.
I have this regular expression: ^(\[\[).+(\]\]) that I want to match for this data that starts just at the beginning of the line as shown below (I do not want to match anything but the things starting at the beginning of a line):
[[match this]] [[don't match this]]
{{Link GA|es}}
{{Link FA|ca}}
And for some reason it is not matching anything in Java (or other regex "testers" such as regexpal.com). By "in Java" i mean with the String.replaceAll(String regex, String replacement) method in the Java String API.
But, if I omit the ^ and just have (\[\[).+(\]\]) it matches fine at the beginning of the line, but also matches inline instances which I do not want.
Can anyone point out what the error is here? Thank you
^ means "start of string", not "start of line", unless you use the Pattern.MULTILINE (or (?m)) option when building the regex. Also, you should be using a lazy quantifier (as pointed out by Dave Newton in his comment).
Finally, don't forget to double the backslashes:
String result = subject.replaceAll("(?m)^\\[\\[.+?\\]\\]", "");
.+ is greedy, in that it will match everything it can (here, matching everything up to the last \]\]
To stop this behaviour just add a ? to make it non-greedy
^\[\[.+?\]\]
Will match [[ then look for any characters until it finds the first occurrence of ]]
(\[\[).+(\]\]){1}+ {1}+ that mean exactly one time's improve link
I have a string which could end with either
"/"
or
"/?secure=true"
I need to validate it. I tried this
\b(/(/?secure=true)?)\b
but I get no matches found with the regex.
However, with
^(/(\$secure=true)?)$
match is found. But this wont help in my case as the string that I want to validate will be prefixed with chars something like "thisismystring/" or "thisismystring/?secure=true".
I want to know how to go about this.
You need to use the pipe symbol (|) to specify an or:
(/|/\?secure=true)$
You also need to escape the question mark, as here, and should specify the "$" for end of string but not the "^" for start of string.
Both cases would contain the
/
So that should not be in the optional part.
You put the optional part in ()?
and escape the question mark with backslash
/(\?secure=true)?
And to indicate that it should end with that, you put $ at the end
/(\?secure=true)?$
Can be done like this:
/(\?secure=true)?$
I need the regex expression to remove any text before a match and including the match
eg. I want to remove "123S" and everything before it, I know I can do this with
string.replaceAll("^.*?(?=[123S])","");
string.replaceAll("123S","");
But really want to do it in a single expression (can't find another example anywhere!)
You can do it with:
string.replaceAll("^.*123S","");
Remove non-greedy ? to match last occurence and .* everything before.
You don't need the look ahead:
"abc123Sdef123Sxyz".replaceAll("^.*?123S","");
This replaces the first occurence only, if that is what you need (output is def123Sxyz).
In case you want to replace up to the last 123S, just remove the ? modifier:
"abc123Sdef123Sxyz".replaceAll("^.*123S","");
Output is xyz.
string.replaceAll("^.*?123S", "");
(?= is the "if followed by" pattern which you don't want, and [123S] isn't even correct it'll catch just '2' for instance.
string.replaceAll("^.*?123S","");
More efficient and improves clarity so someone else knows what you're doing.