• Post Reply Bookmark Topic Watch Topic
  • New Topic

regex77  RSS feed

 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


i would like to match the set of words in the pattern(surya,chandra,amma,c)but it is not working?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

BTW, what do you think the "\\d+[100-200]" part of the pattern is supposed to do? Something tells me that you expect a number between 100 and 200?

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Character classes don't do what you think they do. In a regex, everything between [ and ] stands for just 1 letter. So [surya|chandra|amma|C] is equivalent to [|Cacdhmnrsuy], which means "match exactly one of these characters".

Character classes do not modify other parts of the regex (such as \d+).

Also, why are you using a reluctant quantifier (\w+?)?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yeah. The first part of the regular expression was so "wrong", that I didn't even bother looking at the second part to notice that it was incorrect as well. I think it may be a good idea for the OP to revisit a tutorial on regular expressions, as it is unclear how we can give a hint in the right direction here.

Henry
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The about code matches "101 surya" but it does not match "101 C".I used + to match one or character and ? to match single letter or group please justify?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As others have already said, the regular expression is completely wrong. By accident the syntax of the regular expression is valid, but it means something totally different than what you think it means.

"\\d+[100-200] \\w+?[surya|chandra|amma|C]"

You seem to think that "\\d+[100-200]" matches numbers between 100 and 200. It does not. This means: one more more digits, followed by a character that is 1 or 0 or something in the range 0, 1, 2 or 0 or 0.

Likewise, "\\w+?[surya|chandra|amma|C]" does not mean one or more words that are equal to "surya", "chandra", "amma" or "C". It means: one or more word characters, followed by a character that is one of the characters in the names.

You'd better throw away the regular expression, study the syntax for regular expressions again, see also the tutorial.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:The about code matches "101 surya" but it does not match "101 C".I used + to match one or character and ? to match single letter or group please justify?


As a side note, I am going to speculate that the OP forgot to test what should *not* match, and only tested what should match. Doing so would have likely found the issue earlier. In other words, something like "58475437590 ALargeNumberFollowedByARandomSetOfWords" should not match, but it does!

Henry
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for sharing your views i just solved it by studying following the syntax!
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is the new regex you wrote?
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



[HENRY: Deleted long list of programming language names because it was messing up formatting. And it is also not relevant to discussion]
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the purpose of the carat operator(^) is to perform negation.
^(1[0-2]|[1-9])$
(1[0-2]|[1-9])$
both the above pattern will accept numbers between 1-12 and please explain me the difference?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:As the purpose of the carat operator(^) is to perform negation.
^(1[0-2]|[1-9])$
(1[0-2]|[1-9])$
both the above pattern will accept numbers between 1-12 and please explain me the difference?


The "carat", in the context that it is used, is *not* performing negation -- it is representing the beginning of line boundary.

Henry
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:As the purpose of the carat operator(^) is to perform negation


Sorry but the carat in that position does not do that! In your examples the carat is the beginning of input anchor. The carat as a negation is only applicable in character sets.
 
Campbell Ritchie
Marshal
Posts: 56592
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not carat but caret. No, I do not think the caret causes negation if it is at the very beginning of the regex. Please remind yourself how Java® regular expressions work: here is a tutorial. Look particularly at the section called boundary matchers.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your new regex will not match the string "110 C". It matches 5 or 6 digits (the ? only applies to the last character class).

In my opinion, regular expressions are mostly useful for splitting a string into logical parts, which you can then verify using additional code. Take a look at Matcher.group(String).
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
https://www.hackerrank.com/challenges/hackerrank-language

i just wrote the pattern to solve above problem.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:https://www.hackerrank.com/challenges/hackerrank-language

i just wrote the pattern to solve above problem.


The problem requires the ID to be from 10000 (inclusive) and 100000 (exclusive). Your regular expression will match a *lot* of numbers outside of that range.

Henry
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pattern p=Pattern.compile("([0-9][0-9][0-9][0-9][0-9]0?) (C|CPP|JAVA|PYTHON|DELETED)");
sorry is it correct?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:Pattern p=Pattern.compile("([0-9][0-9][0-9][0-9][0-9]0?) (C|CPP|JAVA|PYTHON|DELETED)");
sorry is it correct?


What have you tried? It should likely take a few minutes to write a program that tries out of range numbers.

Hint: ... and this program should find valid out of range numbers almost immediately.

Henry
 
bairava surya
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

please help me the exception in the above pattern i.e occured due to brackets.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bairava surya wrote:
please help me the exception in the above pattern i.e occured due to brackets.


Well, it would help us if you mentioned what the exception is... but I am going to assume that it has something to do with the unmatched parenthesis?

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tip: Use the Comments flag to ignore whitespace so you can more easily match brackets together: "(?x: () | () | () )"

Protip: Stop using regular expressions to do verification of numbers.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to disallow that a string of digits starts with a 0, use a zero-width negative lookahead.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:
Protip: Stop using regular expressions to do verification of numbers.


Speaking of the number portion of the regular expression, it seems to keep changing with each post by the OP. On the original post, it was supposed to verify a number from 100 to 200, which from the regex, was done incorrectly. On the next post, it was supposed to verify a six digit number from 100000 and higher, which from the regex, was also done incorrectly. With this latest post, the regex verifies a non-zero-leading number from 0 to 180, which I guess, if that is what the OP wanted, would be correct...

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!