Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Match one single digit but leave out sequences of digits

Last post 02-09-2010, 5:26 PM by Aussie Susan. 6 replies.
Sort Posts: Previous Next
  •  02-06-2010, 4:23 PM 59347

    Match one single digit but leave out sequences of digits

    I have a problem that sounds very simple at first:

    I need to match single digits in a string, but leave out sequences made up of more than one consecutive digits.

    The issue is: I build an online dictionary using php/mysql. I use html encoding for the dictionary. Examples:

    (First the string stored in database then how it should look on the webpage)

    autorizált kereskedő (shown on the webpage as autorizált kereskedÅ‘)

    SZISZ1 (shown on the webpage as SZISZ1)

    SZISZ2 I. (shown on the webpage as SZISZ2 I.)

    I need to match the 1 and 2 from the words "SZISZ1" and "SZISZ1 I." to convert them to superscript  but leave out the sequence 337 from the html encoding of special characters in other words. All special characters have a similar code in the database: "&#[0-9]{3}" and all single digits have the form "[0-9]{1}". My problem is that the regular expression used to find a single digit fires separately for each digit of a sequence of digits and it messes up how my special characters shown on screen.

    Please do not tell me to store the superscript html tags in the database.

    Your help will be greatly appreciated.

    Thank you, Tihamer

  •  02-07-2010, 12:15 PM 59372 in reply to 59347

    Re: Match one single digit but leave out sequences of digits

    pls be consistent with your specification:

    you wrote: 

    ***I need to match the 1 and 2 from the words "SZISZ1" and "SZISZ1 I." to convert them to superscript  but leave out the sequence 337 from the html encoding of special characters in other words.***

     the problem is there is no string *337* in the two inputs you gave: "SZISZ1" and "SZISZ1 I."

    keep your spec simple, like

    i got string

    SZISZ1__337XXXi need to only match on

    SZISZ1

    if you give an extra effort to explain it better, you'll most likely get help faster here.

     

  •  02-08-2010, 3:52 AM 59401 in reply to 59372

    Re: Match one single digit but leave out sequences of digits

    Sorry, I try to be explicit now:

    I have a list of words (an online dictionary).

    Examples: 

    szisz1,

    szisz2

    szisz1 II.

    szisztĸm

    I need a regular expression to match and replace the numbers 1, 2 and 1 from the first three words, but not to do anything to the 312 in the fourth word.

    Result what I need:

    szisz<sup>1</sup>

    szisz<sup>2</sup>

    szisz<sup>1</sup> II.

    sziste&#312;m (no change here)

    Thank you.


  •  02-08-2010, 7:02 AM 59404 in reply to 59347

    Re: Match one single digit but leave out sequences of digits

    Found a solution:

    Search pattern: '/([a-zA-Z])(\d)/'

    Replacement pattern: '$1<sup>$2</sup>'

    That is: I search for a sequence made up of one letter [a-zA-Z] plus one digit \d

    and in the replacement I keep the letter ($1) (use it in the search pattern for context only) and add the <sup> </sup> around my digit ($2).

    This is basically scratching my left ear with my right hand and the problem still remains:

    Can you use a regular expression to match ONE AND ONLY ONE digit in a string?

     

  •  02-08-2010, 5:07 PM 59428 in reply to 59404

    Re: Match one single digit but leave out sequences of digits

    Try (untested):

    (?<!\d)\d(?!\d)

    Susan

  •  02-09-2010, 12:12 PM 59507 in reply to 59428

    Re: Match one single digit but leave out sequences of digits

    Thank you for your advice, Susan.

    It does not seem to work, but I got the idea, and it can be refined if tested.

    Tiha

  •  02-09-2010, 5:26 PM 59527 in reply to 59507

    Re: Match one single digit but leave out sequences of digits

    In what way does it "...not seem to work..."? Does it give you syntax or other errors - if so what are they? Does it find too much or not enough or what?

    It might help if you read the posting guidelines (in the sticky note at the beginning of the forum) and told us the regex variant you are using.

    Susan

View as RSS news feed in XML