• Post Reply Bookmark Topic Watch Topic
  • New Topic

What is the meaning of this regular expression?  RSS feed

 
Tiya Khambadkone
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the meaning of the following regular expression ?


Is it 2 asterisks at the beginning of the line followed by a word 'GET' ?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's what it looks like to me.
 
Ramsin Khoshaba
Ranch Hand
Posts: 65
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\\ is an escaped character, so \\* matches 0 or more backslashes.

EDIT: Sorry, I did not notice the String literal.
 
Tiya Khambadkone
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ramsin,

I did not get your answer. Could you tell me what do you think is the meaning of the expression?
do you mean, it has nothing to do with the start of the line ?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that backslash is not only an escape character in a regex, but also in strings.

So the string "^\\*\\*GET" actually means the regex ^\*\*GET

 
Ren Rem
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually it's looking for two or more backslashes at the start followed by the letters "GET" ...

The '^' signifies start of string (position 0) ... the * both signify repeat of the previous character - SOO, you need to "escape" the * for regex to find an actual '*' ... and as already mentioned, the double backslashes signify "escaped-backslashes", meaning you're passing actual backslashes in the string ... with regex, if you want to find "**GET" at the start of input string, you would use "^\*\*GET", BUT to pass that to regex from a String in Java, you need to "escape" the backslash, hence the \\ before each asterix.
 
Tiya Khambadkone
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My job is to get the regular expression from the database. In this case it is \*\*GET Then, I will get a 100 MB file and I have to replace this regular expression with another regular expression \*\*POST

My requirement is
Change "\*\*GET" to "\*\*POST" throughout a file
Sorry, I just saw that there is no caret and no double backslashes.
when I copied the string to my java IDE then automatically another backslash got appended.

I am trying to store the value from database into a string and then I want to use replaceAll(string1, string2). However, i don't understand the meaning of the regular expression.
 
Ramsin Khoshaba
Ranch Hand
Posts: 65
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiya,

You can try regexr, an online tool used to learn and test regular expressions.
Don't forget to hover the cursor over each item in the expression, to get some explanation.
 
Tiya Khambadkone
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. The online tool tells me that my expression matches with **GET and **POST.
I am not sure if that is what it is.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ren Rem wrote:Actually it's looking for two or more backslashes at the start followed by the letters "GET" ...

No, it's not. See my previous post.
 
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
Tiya Khambadkone wrote:
My requirement is
Change "\*\*GET" to "\*\*POST" throughout a file
Sorry, I just saw that there is no caret and no double backslashes.
when I copied the string to my java IDE then automatically another backslash got appended.


If you are trying to represent a backslash, and do so in a string literal, then the IDE did if correctly. You need to add the backslash for escape backslashes with string literals.

Tiya Khambadkone wrote:
I am trying to store the value from database into a string and then I want to use replaceAll(string1, string2). However, i don't understand the meaning of the regular expression.


You might want to consider using the replace() method, that takes two strings, instead. It looks like you are trying to replace a value with another value, *and* you don't understand regular expressions. So, why use regular expressions, when you don't have to?

Henry
 
Tiya Khambadkone
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
You might want to consider using the replace() method, that takes two strings, instead. It looks like you are trying to replace a value with another value, *and* you don't understand regular expressions. So, why use regular expressions, when you don't have to?


Answer :

Tiya Khambadkone wrote:
My job is to get the regular expression from the database. In this case it is \*\*GET Then, I will get a 100 MB file and I have to replace this regular expression with another regular expression \*\*POST
My requirement is
Change "\*\*GET" to "\*\*POST" throughout a file


I can't change my requirement.
 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiya Khambadkone wrote:I can't change my requirement.


It doesn't look to me like you need to know anything about regular expressions if you just want to replace the string "\*\*GET" by the string "\*\*POST" throughout a file. I don't know much about regular expressions myself and I would probably not be able to correctly explain how those two regular expressions work, but that doesn't make any difference. I could easily write some code to read a text file line by line and replace every instance of the first string by the second string, and then write the new versions of the lines out to another file without even knowing that the two strings are regular expressions.

But perhaps your requirement is not to just replace one string by another? Perhaps your requirement is to find places in the file which match the first regular expression and replace "GET" by "POST" in those places? From what I've read so far about your requirement, I wouldn't interpret it that way.
 
Tiya Khambadkone
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True. I don't need to understand the regularExpression and so I wrote :

where fromRegEx will be "\*\*GET"
and toRegEx will be "\*\*POST".

However I need to test my code and write a unit test class and I don't know what should be the value of fileInputString.
 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're just talking about test cases, then you might want to use a test case which contains the string which is to be replaced. Or maybe several such test cases -- like where the string is at the beginning, in the middle, or at the end. You might want to use test cases where similar strings are present, to make sure they aren't erroneously replaced. And so on and so on... but all of this has nothing to do with regular expressions at all, does it?

You'll need to escape the backslashes correctly, of course, but you already did that in your initial post so I'm sure that isn't a problem.
 
Tiya Khambadkone
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should "\*\*GET" match/mean **GET ?
 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might mean that. Or it might not. you should clarify by asking the person who produced the requirements, as it's impossible for anybody else to guess whether they meant the strings you provided most recently to be a sequence of characters surrounded by quotes or to be a Java string literal.
 
Tiya Khambadkone
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get these regEx values from Database. In database table it is varchar2 with value \*\*GET.
Now it is left upto me to decide ,how I need to fetch these values from database and use it in my code.
 
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
Tiya Khambadkone wrote:I get these regEx values from Database. In database table it is varchar2 with value \*\*GET.
Now it is left upto me to decide ,how I need to fetch these values from database and use it in my code.


First, you need to understand the difference between fetching the values from the database and using it as a regular expression. In the first case, it is just a string. It doesn't matter that it is a regular expression, it is merely a string that you need to fetch from the database... which brings us to the second point.

Tiya Khambadkone wrote:True. I don't need to understand the regularExpression and so I wrote :


This is true if you are only fetching it from the database. If you want to use it as a regular expression, then you absolutely need to understand regular expressions. It is really not a good idea to use something that you do not understand.

Henry
 
Tiya Khambadkone
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiya Khambadkone wrote:My job is to get the regular expression from the database. In this case the value in a database table is \*\*GET which is varchar2.
Then, I will get a 100 MB file and I have to replace this regular expression with another regular expression \*\*POST and this value(varchar2) will again be fetched from the database.

My requirement is
Change "\*\*GET" to "\*\*POST" throughout a file


So, if I want to understand the meaning of \*\*GET then can anyone confirm me if it is nothing but 2 asterisks followed by GET or anything else? If anything else then what is the other possibility.

 
Paul Clapham
Sheriff
Posts: 22834
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiya Khambadkone wrote:My requirement is
Change "\*\*GET" to "\*\*POST" throughout a file

So, if I want to understand the meaning of \*\*GET then can anyone confirm me if it is nothing but 2 asterisks followed by GET or anything else? If anything else then what is the other possibility.



The other possibility is that it's exactly what it says it is: a backslash followed by an asterisk followed by a backslash followed by an asterisk followed by the three characters G, E, T.
 
Tiya Khambadkone
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you saying it is not even a regular expression?
If yes, then that is not correct.
It is a regular expression.
 
Tiya Khambadkone
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\*\*GET should match to **GET
Just found out this answer.
 
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
Tiya Khambadkone wrote:are you saying it is not even a regular expression?
If yes, then that is not correct.
It is a regular expression.


I think you are missing the point (or perhaps, we are). The point is, from your description, it is not used as a regular expression. All regular expressions are strings (or they should be), but not all strings are regular expressions. So, it is not that it is not a regular expression -- it is that the database is merely holding a string, that just happens to be a regular expression. You are trying to convert these strings, that again, happens to be a regular expression. You do not need to use regular expressions to convert strings that just happens to be regular expressions.

Tiya Khambadkone wrote:\*\*GET should match to **GET
Just found out this answer.


Yeah. We kinda know that... that was not what we are questioning ...


Anyway, it does kinda feel like we are going in circles. Perhaps, we are not understanding the situation. Perhaps, it might help to try to explain it again.

Henry
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!