2

I am trying to write a regex to get only integer numbers e.g, 23, 234, 45, etc, and not select numbers with decimal points.

For Context :

I need it in a larger regex that I am writing to convert mixed fraction latex input For example:

5\frac{7}{8}

But it should not select latex such as:

3.5\frac{7}{8}

The regex That I have so far is:

(^(.*)(?!(\.))(.*))\\frac{([^{}]+(?:{(?:[^{}]+)}|))}{([^{}]+(?:{(?:[^{}]+)}|))}

But it is for integer and decimal numbers alike. Need to change the regex for group1.

16
  • Quick answer: Change the start to (^(\d*)(?!\.\d).... Commented Jun 18, 2018 at 11:37
  • I tried it but it doesn't work. Commented Jun 18, 2018 at 11:42
  • Yes it does ;) - see this regex101.com/r/iINpRm/1 Commented Jun 18, 2018 at 11:44
  • Improved(?): regex101.com/r/iINpRm/2 Commented Jun 18, 2018 at 11:46
  • Please have a look here https://regexr.com/3r6ic It is showing no matches Commented Jun 18, 2018 at 11:47

1 Answer 1

1

Maybe this will do it for you:

(?<!\d\.)\b(\d+)\\frac{([^{}]+(?:{(?:[^{}]+)}|))}{([^{}]+(?:{(?:[^{}]+)}|))}

It captures an integer expression before \fraq, unless it's preceded by a digit and a full stop.

(?<!\d\.) This ensures the number isn't preceded by a digit followed by a full stop, i.e.
          the integer part of a floating.
\b        Must be at the start of a number (to make sure we don't get a match with the end
          of a multi digit number).
(\d+)     Captures the integer number
\\frac    Matches the string "\fraq"

The rest is the same as you original expression.

See it here at regex101.

Edit

Since there obviously are people out there, however unbelievable, that still haven't moved to a real browser ;) - the answer has to change to:

It depends of the syntax of latex, whether you can do it or not.
(And since I don't know that, I shouldn't have said anything in the first place ;)

The problem is that, without look behinds, you can't do it without matching characters outside the expression your interested in. In your regexr-example you clearly show that you want to be able to match expression not only at the beginning of strings, but also in the middle of the. Thus we need to be able to tell that the part before our match isn't the integer part of a decimal number. Now, doing this with matching isn't a problem. E.g.

(?:^|[^\d.])(\d+)\\frac{...

like Wiktor suggested in comments, will match if the expression is on the start of the line (^), or is preceded by something that isn't a decimal point or a digit ([^\d.]). That should do it. (Here at regex101.)

Well, as pointed out earlier, it depends on the syntax of latex. If two expressions can be directly adjacent, without any operators or stuff between them, you can't (as far as I can tell) do it with JS regex. Consider 1\fraq{0}{1}2\fraq{2}{3} (which I have no idea if it's syntactically correct). The first expression 1\fraq{0}{1} is a piece of a cake. But after that has been matched, we'd need to match a character before the second expression to verify the it doesn't start with a decimal number, but since the first expression already ate the characters, we can't. Because the test (?:^|[^\d.]) to verify that our expression doesn't start with a decimal number, would match one of the characters that actually belongs to our expression (the 2 in 2\fraq{2}{3}), thus making the match fail, because the remaining part doesn't start with the digit needed to satisfy the rest of the regex (\d+)\\frac{....

If, however, an expression always starts the string tested, or is preceded by and operator, or such, then it should be possible using

(?:^|[^\d.])(\d+)\\frac{([^{}]+(?:{(?:[^{}]+)})?)}{([^{}]+(?:{(?:[^{}]+)})?)}

Here at regex101.

(Sorry for my rambling)

Sign up to request clarification or add additional context in comments.

3 Comments

The negative lookbehind operator of RegEx is supported only in the recent Chrome version. It is not supported in any other browsers. Can it be done in any other way without using negative lookbehind operator?
@gvgramazio Justified down (and up) votes is a (necessary?) part of SO to improve quality of, and rate, answers. But they are so much more useful if they indicate the reason for the down vote, because then the answerer has the chance to correct/improve the answer (IMO).
I agree. In fact I tried to suggest the reason, don't know if the real one but surely a possible one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.