<commit_before>from django import forms
from django.utils.translation import ugettext_lazy as _

from blockcypher.constants import COIN_CHOICES
from blockcypher.utils import is_valid_address, is_valid_hash, is_valid_block_num


class SearchForm(forms.Form):
    search_string = forms.CharField(
        required=True,
        min_length=2,
        max_length=128,
    )

    coin_symbol = forms.ChoiceField(
        required=False,
        choices=COIN_CHOICES,
    )

    def clean_search_string(self):
        search_string = self.cleaned_data['search_string'].strip().replace(',', '')

        if is_valid_hash(search_string) or is_valid_address(search_string) or is_valid_block_num(search_string):
            return search_string
        else:
            err_msg = _('Not a valid address, transaction hash, or block number')
            raise forms.ValidationError(err_msg)<commit_msg>better replacing of non alphanumerics<commit_after>from django import forms
from django.utils.translation import ugettext_lazy as _

from blockcypher.constants import COIN_CHOICES
from blockcypher.utils import is_valid_address, is_valid_hash, is_valid_block_num

import re


class SearchForm(forms.Form):
    search_string = forms.CharField(
        required=True,
        min_length=2,
        max_length=128,
    )

    coin_symbol = forms.ChoiceField(
        required=False,
        choices=COIN_CHOICES,
    )

    def clean_search_string(self):
        search_string = self.cleaned_data['search_string'].strip()
        # get rid of non alphanumerics
        search_string = re.sub(r'[^a-zA-Z0-9]+', '', search_string)

        if is_valid_hash(search_string) or is_valid_address(search_string) or is_valid_block_num(search_string):
            return search_string
        else:
            err_msg = _('Not a valid address, transaction hash, or block number')
            raise forms.ValidationError(err_msg)
{'fraction_non_alphanumeric': 0.07813319349764027, 'fraction_numerical': 0.005243838489774515, 'mean_word_length': 3.386206896551724, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2177123', 'n_tokens_mistral': 587, 'n_tokens_neox': 566, 'n_words': 123}