Question:
Regular Expression Help please!?
1970-01-01 00:00:00 UTC
Regular Expression Help please!?
Three answers:
?
2016-10-11 03:43:42 UTC
right this is what it potential: / it extremely is the commencing up of the reg expr ^ shows the tournament might desire to start up on the commencing up of a line of textual content cloth (11+) the parens are used to team words. as a result, it extremely is going to tournament a "a million", observed by potential of various of "a million"s. a million+ the "" is an get away char, turning off the particular which potential of reg expr development chars. as a result, that's superfluous. It in basic terms says tournament a "a million". this could be an errors. The + says tournament various of of those. $ tournament on the top of the textual content cloth line So, all this subject is doing is in basic terms attempting to tournament strains that have a minimum of three "a million"s like this: "111" it could have in basic terms been written as /^111+$/. as a result, the two you probably did not reproduction it properly, or the author made an errors.
Eric V
2008-05-10 10:10:44 UTC
Here's what it would look like in perl regular expression. I'm pretty sure it's similar to java, if not the same. nonetheless this might help you out:



/^\d+(\.\d+(E[-+]?\d+)?)?$/



character meanings:

^ start of string

\d any digit character wildcard

+ match 1 or more times

\. a period, (instead of the any character wildcard)

? match 1 or 0 times

$ end of string



i put the ? after both inner sets of parentheses, which shows that the items inside are optional (match 0 or 1 times)



good luck,

-Eric
Gary
2008-05-10 09:18:42 UTC
I'm assuming Java is the language...



You are on the right track. I might approach this a little differently.



Start with the only known part that MUST exist to create the what you are searching for. That is, the expression must start with digits.



\d+



\d means digits (the same as [0-9]) and the + is one or more times. Now, before we move forward... we have to consider that this is not all there is to it. For example, someone could have those digits embedded in text (type-o or modern slang). In that case we would not want to assume the digits are a decimal literal.



\b\d+\b



Here, we simple add \b (word boundary) which will mean that we will match the value 2 but not 2cool. We could approach this in other ways, but you would have to be more specific about the input you will be passing to the RegExp.



Next, we have to consider that everything else is optional. So... lets tackle the decimal with trailing digits.



\b\d+(?:\.\d+)?\b



What have we done here? In a RegEx (X) means we are concerned about whatever X is and we want to remember it. If we use (?:X) then we are saying we care about X but we do not want to remember the match. Also, we have to consider that the decimal and following digits may not be there... so we but the ? at the end of the group to indicate we want to match once or not at all. Finally, the decimal itself is a construct meaning "any character" so we have to escape it with a backslash.



Finally, we have the E +/- followed by one or more digits. This part only appears if we have the decimal. And, it may not even be there. So...



\b\d+(?:\.\d+(?:E\+|-\d+)?)?\b



Since the E +/- and following digits only appear if we have the decimal positions, we put another (?:X) grouping inside of the existing (?:X) grouping. This is a way of saying, we care about the E +/- stuff if the .xxx stuff is there. Our expression for this is E (literally the letter) followed by \+|- (plus or minus... and we escape the + since it has other meanings if we don't) \d+ (one or more digits). We end that grouping with a ? meaning it should show up once or not at all.



So the final expression is





\b

... word bundary

\d+

... one or more digits

(?:

... here comes a group of things, but don't remember it

\.\d+

... a decimal followed by one or more numbers

(?:

... here comes another group we don't care to remember

E\+|-\d+

... E and a + or - and then one or more digits

)?

... ok... that E +/- thing is there once or not at all

)?

... oh... and the decimal, number thing is there once or not at all



Regular Expressions are tough. No doubt. There are probably also a hundred other ways to do this so my answer may not be the most efficient.



NOTE --- I write this without actually testing it as I went... just going from memory. I may have missed something obvious or just plain done something stupid... do not take this advice without a grain of salt. Be sure to test and re-test each step of this expression as you build it. If I did screw up (quite possible) it will give you an excellent chance to explore and debug the expression. Use the source below for more info.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...